home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / AppsToGo / AppsToGo.src / DTS.Lib / TextEditControl.c < prev    next >
Encoding:
Text File  |  1993-06-18  |  75.2 KB  |  2,931 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         texteditcontrol.c
  5. ** Written by:      Eric Soldan
  6. ** Based on:        TESample, by Bryan Stearns
  7. ** Suggestions by:  Forrest Tanaka, Dave Radcliffe
  8. **
  9. ** Copyright © 1990-1993 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13. /* You may incorporate this sample code into your applications without
  14. ** restriction, though the sample code has been provided "AS IS" and the
  15. ** responsibility for its operation is 100% yours.  However, what you are
  16. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  17. ** after having made changes. If you're going to re-distribute the source,
  18. ** we require that you make it clear in the source that the code was
  19. ** descended from Apple Sample Code, but that you've made changes. */
  20.  
  21. /* This is a control implementation of TextEdit.  The advantages to this are:
  22. **
  23. ** 1) Makes using TextEdit in a non-dialog window easy.
  24. ** 2) The TextEdit record is automatically associated with the window, since
  25. **    it is in the window's control list.
  26. ** 3) The TextEdit control can have scrollbars associated with it, and these
  27. **    are also kept in the window's control list.
  28. ** 4) Updating of the TextEdit record is much simpler, since all that is
  29. **    necessary is to draw the control (or all the window's controls with
  30. **    a DrawControls call).
  31. ** 5) There are simple calls to handle TextEdit events.
  32. ** 6) Undo is already supported.
  33. ** 7) A document length can be specified.  This length will not be exceeded
  34. **    when editing the TextEdit record.
  35. ** 8) When you close the window, the TextEdit record is disposed of.
  36. **    (This automatic disposal can easily be defeated.)
  37. **
  38. **
  39. ** To create a TextEdit control, you only need a single call.  For example:
  40. **
  41. **    CTENew(rViewCtl,            Resource ID of view control for TextEdit control.
  42. **           true,                 Control initially visible.
  43. **           window,                Window to hold TERecord.
  44. **           &teHndl,                Return handle for TERecord.
  45. **           &ctlRct,                Rect for TextEdit view control.
  46. **           &destRct,            destRct for TERecord
  47. **           &viewRct,            viewRct for TERecord
  48. **           &borderRct,            Used to frame a border.
  49. **           32000,                Max size for TERecord text.
  50. **           cteVScrollLessGrow    TERecord read-write, with vertical scroll
  51. **                                that leaves space for grow box.
  52. **    );
  53. **
  54. ** If you create a TextEdit control that is read-only, you will not be able
  55. ** to edit it, of course.  There will also be no blinking caret for that
  56. ** TextEdit control.  You will be able to select text and copy to the
  57. ** clipboard, but that is all.
  58.  
  59. ** Simply create destRct, viewRct, and borderRct appropriately, and
  60. ** then call CTENew (which stands for Control TENew).  If teHndl is returned
  61. ** nil, then CTENew failed.  Otherwise, you now have a TextEdit control in
  62. ** the window.
  63. **
  64. ** NOTE: There is a TextEdit bug (no way!!) such that you may need to set the
  65. **       viewRct right edge 2 bigger than the right edge of destRct.  If you
  66. **       do not do this, then there will be some clipping on the right edge in
  67. **       some cases.  Of course, you may want this.  You may want horizontal
  68. **       scrolling, and therefore you would want the destRct substantially
  69. **       larger than the viewRct.  If you don't want horizontal scrolling,
  70. **       then you probably don't want any clipping horizontally, and therefore
  71. **       you will need to set destRct.right 2 less than viewRct.right.
  72. **
  73. **
  74. ** If the CTENew call succeeds, you then have a TextEdit control in your
  75. ** window.  It will be automatically disposed of when you close the window.
  76. ** If you don't waht this to happen, then you can detach it from the
  77. ** view control which owns it.  To do this, you would to the following:
  78. **
  79. **  viewCtl = CTEViewFromTE(theTextEditHndl);
  80. **  if (viewCtl) SetCRefCon(viewCtl, nil);
  81. **
  82. ** The view control keeps a reference to the TextEdit record in the refCon.
  83. ** If the refCon is cleared, then the view control does nothing.  So, all that
  84. ** is needed to detach a TextEdit record from a view control is to set the
  85. ** view control's refCon nil.  Now if you close the window, you will still
  86. ** have the TextEdit record.
  87. **
  88. **
  89. ** To remove a TextEdit control completely from a window, you make one call:
  90. **
  91. **  CTEDispose(theTextEditHndl);
  92. **
  93. ** This disposes of the TextEdit record, the view control, and any scrollbar
  94. ** controls that were created when the TextEdit control was created with
  95. ** the call CTENew.
  96. **
  97. **
  98. ** Events for TextEdit record are handled nearly automatically.  You can
  99. ** make one of 3 calls:
  100. **
  101. **  CTEClick(window, eventPtr, &action);
  102. **  CTEEvent(window, eventPtr, &action);
  103. **  CTEKey(window, eventPtr);
  104. **
  105. ** In each case, if the event was handled, true is returned.  CTEEvent simply
  106. ** calls either CTEClick or CTEKey, whichever is appropriate.
  107. **
  108. **
  109. ** Another call you will want to use is CTEEditMenu.  This is used to set the
  110. ** state of cut/copy/paste/clear for TextEdit controls.  It checks the active
  111. ** control to see if text is selected, if the control is read-only, etc.
  112. ** Based on this information, it sets cut/copy/paste/clear either active
  113. ** or inactive.  If any menu items are set active, it returns true.
  114. **
  115. **
  116. ** One more high-level call is CTEUndo().  In response to an undo menu item
  117. ** being selected by the user, just call CTEUndo(), and the edits the user
  118. ** has made will be undone.  (This includes undoing an undo.)
  119. **
  120. **
  121. ** The last high-level call is CTEClipboard.  Call it when you want to do a
  122. ** cut/copy/paste/clear for the active TextEdit control.  The value to pass
  123. ** is as follows:
  124. **
  125. **  2: cut
  126. **  3: copy
  127. **  4: paste
  128. **  5: clear
  129. **
  130. ** These are the same values you would pass to a DA for these actions.
  131. */
  132.  
  133.  
  134.  
  135. /*****************************************************************************/
  136.  
  137.  
  138.  
  139. #ifndef __TEXTEDITCONTROL__
  140. #include "TextEditControl.h"
  141. #endif
  142.  
  143. #ifndef __BALLOONS__
  144. #include <Balloons.h>
  145. #endif
  146.  
  147. #ifndef __CONTROLS__
  148. #include <Controls.h>
  149. #endif
  150.  
  151. #ifndef __DTSLib__
  152. #include "DTS.Lib.h"
  153. #endif
  154.  
  155. #ifndef __ERRORS__
  156. #include <Errors.h>
  157. #endif
  158.  
  159. #ifndef __EVENTS__
  160. #include <Events.h>
  161. #endif
  162.  
  163. #ifndef __GESTALTEQU__
  164. #include <GestaltEqu.h>
  165. #endif
  166.  
  167. #ifndef __MEMORY__
  168. #include <Memory.h>
  169. #endif
  170.  
  171. #ifndef __MENUS__
  172. #include <Menus.h>
  173. #endif
  174.  
  175. #ifndef __OSEVENTS__
  176. #include <OSEvents.h>
  177. #endif
  178.  
  179. #ifndef __OSUTILS__
  180. #include <OSUtils.h>
  181. #endif
  182.  
  183. #ifndef __RESOURCES__
  184. #include <Resources.h>
  185. #endif
  186.  
  187. #ifndef __SCRAP__
  188. #include <Scrap.h>
  189. #endif
  190.  
  191. #ifndef __SCRIPT__
  192. #include <Script.h>
  193. #endif
  194.  
  195. #ifndef __TEXTSERVICES__
  196. #include <TextServices.h>
  197. #endif
  198.  
  199. #ifndef __TSMTE__
  200. #include "TSMTE.h"
  201. #endif
  202.  
  203. #ifndef __UTILITIES__
  204. #include "Utilities.h"
  205. #endif
  206.  
  207.  
  208.  
  209. /*****************************************************************************/
  210.  
  211.  
  212.  
  213. typedef struct cdefRsrcJMP {
  214.     long    moveInst;
  215.     long    jsrInst;
  216.     short    jmpInst;
  217.     long    jmpAddress;
  218. } cdefRsrcJMP;
  219. typedef cdefRsrcJMP *cdefRsrcJMPPtr, **cdefRsrcJMPHndl;
  220.  
  221.  
  222.  
  223. /*****************************************************************************/
  224.  
  225.  
  226.  
  227. short    gTECtl = rTECtl;
  228.  
  229.  
  230. short    CTEGetLineNum(TEHandle te, short offset);
  231. short    CTEGetLineHeight(TEHandle te, short lineNum, short *ascent);
  232.  
  233. static pascal long        CTECtl(short varCode, ControlHandle ctl, short msg, long parm);
  234. static Boolean            GoFast(TEHandle teHndl, EventRecord *event);
  235. static pascal void        TSMTEUpdateProc(TEHandle te, long fixLen, long inputAreaStart,
  236.                                         long inputAreaEnd, long pinStart, long pinEnd, long refCon);
  237. static cdefRsrcJMPHndl    gCDEF;
  238. static Boolean            gCanGoSlow;
  239.  
  240.  
  241.  
  242. /*****************************************************************************/
  243.  
  244.  
  245.  
  246. static TEHandle            gActiveTEHndl;
  247.     /* Currently active TextEdit record.  (nil if none active.) */
  248.  
  249. static TEHandle            gFoundTEHndl;
  250.     /* Global value used to return info from the TextEdit control proc. */
  251.  
  252. static ControlHandle    gFoundViewCtl;
  253.     /* Global value used to return info from the TextEdit control proc. */
  254.  
  255. static pascal void        VActionProc(ControlHandle scrollCtl, short part);
  256. static pascal void        HActionProc(ControlHandle control, short part);
  257. static void                AdjustOneScrollValue(TEHandle teHndl, ControlHandle ctl, Boolean vert);
  258.  
  259. ClikLoopProcPtr        gDefaultClikLoop;
  260.     /* The clikLoop TextEdit wants to use.  Our custom clikLoop must call
  261.     ** this as well.  The default TextEdit clikLoop is stored here.
  262.     */
  263.  
  264. #define kTELastForWind    1
  265. #define kCrChar            13
  266.  
  267.  
  268.  
  269. static void                dummyCTEActivate(Boolean active, TEHandle teHndl);
  270. static Boolean            dummyCTEClick(WindowPtr window, EventRecord *event, short *action);
  271. static ControlHandle    dummyCTECtlHit(void);
  272. static TEHandle            dummyCTEFindActive(WindowPtr window);
  273. static short            dummyCTEKey(WindowPtr window, EventRecord *event);
  274. static ControlHandle    dummyCTENext(WindowPtr window, TEHandle *teHndl, ControlHandle ctl, short dir, Boolean justActive);
  275. static void                dummyCTESetSelect(short start, short end, TEHandle teHndl);
  276. static ControlHandle    dummyCTEViewFromTE(TEHandle teHndl);
  277. static TEHandle            dummyCTEWindActivate(WindowPtr window, Boolean displayIt);
  278.  
  279. CTEActivateProcPtr           gcteActivate           = dummyCTEActivate;
  280. CTEClickProcPtr              gcteClick              = dummyCTEClick;
  281. CTECtlHitProcPtr             gcteCtlHit             = dummyCTECtlHit;
  282. CTEFindActiveProcPtr         gcteFindActive         = dummyCTEFindActive;
  283. CTEKeyProcPtr                gcteKey                = dummyCTEKey;
  284. CTENextProcPtr               gcteNext               = dummyCTENext;
  285. CTESetSelectProcPtr          gcteSetSelect          = dummyCTESetSelect;
  286. CTEViewFromTEProcPtr         gcteViewFromTE         = dummyCTEViewFromTE;
  287. CTEWindActivateProcPtr       gcteWindActivate       = dummyCTEWindActivate;
  288.  
  289.  
  290.  
  291. /*****************************************************************************/
  292. /*****************************************************************************/
  293. /*****************************************************************************/
  294.  
  295.  
  296.  
  297. /* Instead of calling the functions directly, you can reference the global
  298. ** proc pointers that reference the functions.  This keeps everything from
  299. ** being linked in.  The default global proc pointers point to dummy functions
  300. ** that behave as if there aren't any TextEdit controls.  The calls can still be
  301. ** made, yet the runtime behavior is such that it will operate as if there
  302. ** no instances of the TextEdit control.  This allows intermediate code to access
  303. ** the functions or not without automatically linking in all sorts of stuff
  304. ** into the application that isn't desired.  To change the global proc pointers
  305. ** so that they point to the actual functions, just call CTEInitialize() once
  306. ** in the beginning of the application.  If CTEInitialize() is referenced, it will
  307. ** get linked in.  In turn, everything that it references directly or indirectly
  308. ** will get linked in. */
  309.  
  310. #pragma segment Controls
  311. void    CTEInitialize(void)
  312. {
  313.     if (gcteActivate != CTEActivate) {
  314.         gcteActivate           = CTEActivate;
  315.         gcteClick              = CTEClick;
  316.         gcteCtlHit             = CTECtlHit;
  317.         gcteFindActive         = CTEFindActive;
  318.         gcteKey                = CTEKey;
  319.         gcteNext               = CTENext;
  320.         gcteSetSelect          = CTESetSelect;
  321.         gcteViewFromTE         = CTEViewFromTE;
  322.         gcteWindActivate       = CTEWindActivate;
  323.  
  324.         CTEConvertClipboard(true, true);
  325.     }
  326. }
  327.  
  328.  
  329.  
  330. /*****************************************************************************/
  331.  
  332.  
  333.  
  334. /* Activate this TextEdit record.  If another is currently active, deactivate
  335. ** that one.  The view control for this TextEdit record is also flagged to
  336. ** indicate which was the last active one for this window.  If the previous
  337. ** active TextEdit record was in the same window, then flag the old one off
  338. ** for this window.  The whole point for this per-window flagging is so that
  339. ** activate events can reactivate the correct TextEdit control per window. */
  340.  
  341. #pragma segment Controls
  342. void    CTEActivate(Boolean active, TEHandle teHndl)
  343. {
  344.     WindowPtr        tePort, oldPort;
  345.     ControlHandle    viewCtl;
  346.     TEHandle        te;
  347.     CTEDataHndl        teData;
  348.     short            oldDisplay, newDisplay;
  349.  
  350.     if (!teHndl) return;
  351.  
  352.     teData = nil;
  353.     if (viewCtl = CTEViewFromTE(teHndl))
  354.         teData = (CTEDataHndl)(*viewCtl)->contrlData;
  355.  
  356.     if (!active) {
  357.         GetPort(&oldPort);
  358.         SetPort(tePort = (*teHndl)->inPort);
  359.         if(TSMTEAvailable())
  360.             DeactivateTSMDocument((*teData)->docID);
  361.         TEDeactivate(teHndl);
  362.         if (teHndl == gActiveTEHndl)
  363.             gActiveTEHndl = nil;
  364.         if (viewCtl)
  365.             CTEUpdate(teHndl, viewCtl, true);
  366.         SetPort(oldPort);
  367.         return;
  368.     }
  369.  
  370.     if (!viewCtl) return;
  371.  
  372.     (*teData)->mode |= cteActive;
  373.     GetPort(&oldPort);
  374.     SetPort(tePort = (*teHndl)->inPort);
  375.  
  376.     if(TSMTEAvailable())
  377.         ActivateTSMDocument((*teData)->docID);
  378.     TEActivate(gActiveTEHndl = teHndl);
  379.  
  380.     CTEUpdate(teHndl, viewCtl, true);
  381.     SetPort(oldPort);
  382.         /* Let TextEdit know that it is supposed to be active. */
  383.  
  384.     for (viewCtl = nil;;) {
  385.         viewCtl = CTENext(tePort, &te, viewCtl, 1, false);
  386.         if (!viewCtl) break;
  387.         if (te != teHndl) {
  388.             teData = (CTEDataHndl)(*viewCtl)->contrlData;
  389.             oldDisplay = (*teData)->mode;
  390.             newDisplay = (oldDisplay & (0xFFFF - cteActive));
  391.             if (oldDisplay != newDisplay) {
  392.                 (*teData)->mode = newDisplay;
  393.                 CTEActivate(false, te);
  394.             }
  395.         }
  396.     }
  397. }
  398.  
  399.  
  400.  
  401. static void    dummyCTEActivate(Boolean active, TEHandle teHndl)
  402. {
  403. #pragma unused (active, teHndl)
  404. }
  405.  
  406.  
  407.  
  408. /*****************************************************************************/
  409.  
  410.  
  411.  
  412. /* This is called when a mouseDown occurs in the content of a window.  It
  413. ** returns true if the mouseDown caused a TextEdit action to occur.  Events
  414. ** that are handled include if the user clicks on a scrollbar that is
  415. ** associated with a TextEdit control. */
  416.  
  417. #pragma segment Controls
  418. Boolean    CTEClick(WindowPtr window, EventRecord *event, short *action)
  419. {
  420.     WindowPtr        oldPort;
  421.     Point            mouseLoc;
  422.     TEHandle        te, teActive;
  423.     ControlHandle    ctlHit, viewCtl;
  424.     CTEDataHndl        teData;
  425.     Boolean            vert;
  426.     short            extendSelect, part, value, newValue, lh, mode;
  427.  
  428.     if (action)
  429.         *action = 0;
  430.  
  431.     GetPort(&oldPort);
  432.     if (!((WindowPeek)window)->hilited) return(false);
  433.  
  434.     SetPort(window);
  435.     mouseLoc = event->where;
  436.     GlobalToLocal(&mouseLoc);
  437.  
  438.     if (!(viewCtl = CTEFindCtl(window, event, &te, &ctlHit))) return(false);
  439.  
  440.     if (viewCtl == ctlHit) {
  441.             /* See if the user clicked directly on the view control for a
  442.             ** TextEdit record.  If so, we definitely have some work to do. */
  443.         if (te != gActiveTEHndl) {
  444.             CTEActivate(true, te);
  445.             if (action)
  446.                 *action = -1;
  447.             SetPort(oldPort);
  448.             return(true);
  449.                 /* If user clicked on TextEdit control other than the
  450.                 ** currently active control, then activate it.  This is our
  451.                 ** only action in this case. */
  452.         }
  453.         extendSelect = ((event->modifiers & shiftKey) != 0);
  454.             /* Extend-select may be occuring. */
  455.  
  456.         gCanGoSlow = true;
  457.             /* There is a slow-zone around the TextEdit area for slow extend-select.
  458.             ** Allow this zone to slow down selection. */
  459.  
  460.             if (TSMTEAvailable()) {
  461.                 if (!TSMEvent(event))
  462.                     TEClick(mouseLoc, extendSelect, te);
  463.             }
  464.             else TEClick(mouseLoc, extendSelect, te);
  465.                 /* Do the extend-select thing.  Most of the work is handled by
  466.                 ** TextEdit.  The only thing we have to do is to update the
  467.                 ** scrollbars while the user is extending the select.  This is
  468.                 ** taken care of by our custom clikLoop procedure. */
  469.  
  470.         teData = (CTEDataHndl)(*viewCtl)->contrlData;
  471.         (*teData)->newUndo = true;
  472.  
  473.         SetPort(oldPort);
  474.         return(true);
  475.     }
  476.  
  477. /* We didn't hit the view control for a TextEdit record, but don't give up yet.
  478. ** The user may be clicking on a related scrollbar.  Let's find out... */
  479.  
  480.     if (WhichControl(mouseLoc, event->when, window, &ctlHit)) {
  481.             /* The user did click on a control.  But is it a scrollbar
  482.             ** for a TextEdit control?  Stay tuned... */
  483.  
  484.         te = CTEFromScroll(ctlHit, &viewCtl);
  485.  
  486.         if (te) {        /* It was a related scrollbar. */
  487.  
  488.             if (te != gActiveTEHndl) {
  489.                 CTEActivate(true, te);
  490.                 if (action)
  491.                     *action = -1;
  492.                 SetPort(oldPort);
  493.                 return(true);
  494.                     /* If user clicked on TextEdit control other than the
  495.                     ** currently active control, then activate it.  This is our
  496.                     ** only action in this case. */
  497.             }
  498.  
  499.             vert = ((*ctlHit)->contrlRect.top <= (*viewCtl)->contrlRect.top);
  500.                 /* Horizontal or vertical scroll.  Only the rect knows. */
  501.  
  502.             part = FindControl(mouseLoc, window, &ctlHit);
  503.  
  504.             switch (part) {
  505.                 case 0:        /* Inactive scrollbar. */
  506.                     break;
  507.                 case inThumb:
  508.                     value = GetCtlValue(ctlHit);
  509.                     part  = TrackControl(ctlHit, mouseLoc, nil);
  510.                     if (part) {
  511.                         newValue = GetCtlValue(ctlHit);
  512.                         if (value != newValue) {    /* If scrollbar value changed... */
  513.                             if (vert) {
  514.                                 teData = (CTEDataHndl)(*viewCtl)->contrlData;
  515.                                 mode   = (*teData)->mode;
  516.                                 if (mode & cteScrollFullLines) {
  517.                                     if (!(mode & cteStyledTE)) {
  518.                                         if (lh = CTEGetLineHeight(te, 1, nil)) {
  519.                                             newValue += (lh >> 1);
  520.                                             newValue /= lh;
  521.                                             newValue *= lh;
  522.                                             SetCtlValue(ctlHit, newValue);
  523.                                         }
  524.                                     }
  525.                                 }
  526.                                 TEScroll(0, value - newValue, te);
  527.                             }
  528.                             else
  529.                                 TEScroll(value - newValue, 0, te);
  530.                         }
  531.                     }
  532.                     break;
  533.  
  534.                 default:
  535.                     teActive = gActiveTEHndl;
  536.                     gActiveTEHndl = te;
  537.                         /* This is a hack way to pass the action procedure
  538.                         ** which TextEdit record we are dealing with.
  539.                         */
  540.                     if (vert)
  541.                         TrackControl(ctlHit, mouseLoc, (ProcPtr)VActionProc);
  542.                     else
  543.                         TrackControl(ctlHit, mouseLoc, (ProcPtr)HActionProc);
  544.  
  545.                     gActiveTEHndl = teActive;
  546.                         /* Unhack our previous hack. */
  547.                     break;
  548.             }
  549.  
  550.             SetPort(oldPort);
  551.             return(true);
  552.         }
  553.     }
  554.  
  555.     SetPort(oldPort);
  556.     return(false);
  557. }
  558.  
  559.  
  560.  
  561. static Boolean    dummyCTEClick(WindowPtr window, EventRecord *event, short *action)
  562. {
  563. #pragma unused (window, event)
  564.  
  565.     if (action)
  566.         *action = 0;
  567.     return(false);
  568. }
  569.  
  570.  
  571.  
  572. /*****************************************************************************/
  573.  
  574.  
  575.  
  576. /* This is the custom clikLoop, which is called from the assembly glue code.
  577. ** This handles updating the scrollbars as the user is drag-selecting in
  578. ** the TextEdit control. */
  579.  
  580. #pragma segment Controls
  581. void    CTEClikLoop(void)
  582. {
  583.     WindowPtr        oldPort, window;
  584.     TEHandle        te;
  585.     Point            mouseLoc;
  586.     Rect            viewRct;
  587.     RgnHandle        rgn;
  588.     short            dl, dr, dt, db;
  589.     long            tick;
  590.  
  591.     GetPort(&oldPort);
  592.     SetPort(window = (WindowPtr)(*gActiveTEHndl)->inPort);
  593.  
  594.     te = gActiveTEHndl;
  595.         /* This better be what the user is dragging, or we did something
  596.         ** wrong elsewhere.
  597.         */
  598.  
  599.     GetMouse(&mouseLoc);
  600.     viewRct = (*te)->viewRect;
  601.  
  602.     if (!PtInRect(mouseLoc, &viewRct)) {
  603.         /* User is outside the TextEdit area, so scrolling is happening. */
  604.  
  605.         if (tick = gCanGoSlow)
  606.             tick = TickCount();
  607.                 /* As an extra feature, there is a zone around the TextEdit
  608.                 ** viewRct that the scroll will be slowed down.  If the user
  609.                 ** drags outside the viewRct outside this zone, then scrolling
  610.                 ** occurs as fast as possible. */
  611.  
  612.         rgn = NewRgn();
  613.         GetClip(rgn);
  614.         ClipRect(&(window->portRect));
  615.             /* The clipRgn is set to protect everything outside viewRct.
  616.             ** This doesn't work very well when we want to change
  617.             ** the scrollbars.  Save the old and open it up.
  618.             */
  619.  
  620.         dl = viewRct.left - mouseLoc.h;
  621.         dr = mouseLoc.h   - viewRct.right;
  622.         dt = viewRct.top  - mouseLoc.v;
  623.         db = mouseLoc.v   - viewRct.bottom;
  624.             /* Check the delta value for each side of viewRct.  This will
  625.             ** be used to determine if we should scroll fast or slow.
  626.             */
  627.  
  628.         CTEAdjustScrollValues(te);
  629.             /* Scroll them puppies. */
  630.  
  631.         SetClip(rgn);                                /* restore clip */
  632.         DisposeRgn(rgn);
  633.             /* Make Mr. clipRgn happy again. */
  634.  
  635.         if ((dl < 16) && (dr < 16) && (dt < 16) && (db < 16))
  636.             while (tick + 10 > TickCount());
  637.                 /* Do it really slow.  (This is important on very fast machines.) */
  638.     }
  639.  
  640.     SetPort(oldPort);
  641. }
  642.  
  643.  
  644.  
  645. /*****************************************************************************/
  646.  
  647.  
  648.  
  649. /* Do the cut/copy/paste/clear operations for the currently active
  650. ** TextEdit control.  Caller assumes appropriateness of the call.  Typically,
  651. ** this routine won't be called at an inappropriate time, since the menu
  652. ** item should be enabled or disabled correctly.
  653. ** Use CTEEditMenu to set the menu items undo/cut/copy/paste/clear correctly
  654. ** for the active TextEdit control.  Since undo isn't currently supported,
  655. ** all that CTEEditMenu does for the undo case is to deactivate it right now. */
  656.  
  657. #pragma segment Controls
  658. void    CTEClipboard(short menuID)
  659. {
  660.     WindowPtr        oldPort;
  661.     TEHandle        te;
  662.     ControlHandle    viewCtl;
  663.     CTEDataHndl        teData;
  664.     long            maxTextLen, charsToAdd;
  665.  
  666.     if (!(te = gActiveTEHndl)) return;
  667.     if (!(viewCtl = CTEViewFromTE(te))) return;
  668.  
  669.     GetPort(&oldPort);
  670.     SetPort((*te)->inPort);
  671.     switch (menuID) {
  672.         case 2:
  673.             CTENewUndo(viewCtl, true);
  674.             TECut(te);
  675.             break;
  676.         case 3:
  677.             TECopy(te);
  678.             break;
  679.         case 4:
  680.             if (viewCtl) {
  681.                 teData = (CTEDataHndl)(*viewCtl)->contrlData;
  682.                 maxTextLen  = (*teData)->maxTextLen;
  683.                 charsToAdd  = TEGetScrapLen();
  684.                 charsToAdd -= ((*te)->selEnd - (*te)->selStart);
  685.                 if ((*te)->teLength + charsToAdd <= maxTextLen) {
  686.                     CTENewUndo(viewCtl, true);
  687.                     if ((*teData)->mode & cteStyledTE)
  688.                         TEStylPaste(te);
  689.                     else
  690.                         TEPaste(te);
  691.                 }
  692.             }
  693.             break;
  694.         case 5:
  695.             CTENewUndo(viewCtl, true);
  696.             TEDelete(te);
  697.             break;
  698.     }
  699.  
  700.     CTEAdjustTEBottom(te);
  701.     CTEAdjustScrollValues(te);
  702.     SetPort(oldPort);
  703. }
  704.  
  705.  
  706.  
  707. /*****************************************************************************/
  708.  
  709.  
  710.  
  711. #pragma segment Controls
  712. void    CTEConvertClipboard(Boolean convertClipboard, Boolean becomingActive)
  713. {
  714. #pragma unused (convertClipboard, becomingActive)
  715. }
  716.  
  717.  
  718.  
  719. /*****************************************************************************/
  720.  
  721.  
  722.  
  723. #pragma segment Controls
  724. static pascal long    CTECtl(short varCode, ControlHandle ctl, short msg, long parm)
  725. {
  726. #pragma unused (varCode)
  727.  
  728.     Rect            viewRct;
  729.     TEHandle        te;
  730.     CTEDataHndl        teData;
  731.     ControlHandle    scrollCtl;
  732.     StScrpHandle    uStyl;
  733.     short            i;
  734.     TSMDocumentID    docID;
  735.  
  736.     if (te = (TEHandle)GetCRefCon(ctl))
  737.         viewRct = (*ctl)->contrlRect;
  738.     else
  739.         SetRect(&viewRct, 0, 0, 0, 0);
  740.  
  741.     switch (msg) {
  742.         case drawCntl:
  743.             CTEUpdate(te, ctl, false);
  744.             break;
  745.  
  746.         case testCntl:
  747.             if (PtInRect(*(Point *)&parm, &viewRct)) {
  748.                 gFoundViewCtl = ctl;
  749.                 gFoundTEHndl  = te;
  750.                 return(1);
  751.             }
  752.             return(0);
  753.             break;
  754.  
  755.         case calcCRgns:
  756.         case calcCntlRgn:
  757.             if (msg == calcCRgns)
  758.                 parm &= 0x00FFFFFF;
  759.             RectRgn((RgnHandle)parm, &viewRct);
  760.             break;
  761.  
  762.         case initCntl:
  763.             break;
  764.  
  765.         case dispCntl:
  766.             if (te) {
  767.                 if (te == gActiveTEHndl)
  768.                     gActiveTEHndl = nil;
  769.                 TEDispose(te);
  770.                 if (teData = (CTEDataHndl)(*ctl)->contrlData) {
  771.                     if ((*teData)->undoText)
  772.                         DisposeHandle((Handle)(*teData)->undoText);
  773.                     if (uStyl = (*teData)->undoStyl)
  774.                         DisposeHandle((Handle)uStyl);
  775.                     if (docID = (*teData)->docID)
  776.                         DeleteTSMDocument(docID);
  777.                     DisposeHandle((Handle)teData);
  778.                 }
  779.                 for (i = 0; i < 2; ++i)
  780.                     if (scrollCtl = CTEScrollFromView(ctl, i))
  781.                         DisposeControl(scrollCtl);
  782.             }
  783.             break;
  784.  
  785.         case posCntl:
  786.             break;
  787.  
  788.         case thumbCntl:
  789.             break;
  790.  
  791.         case dragCntl:
  792.             break;
  793.  
  794.         case autoTrack:
  795.             break;
  796.     }
  797.  
  798.     return(0);
  799. }
  800.  
  801.  
  802.  
  803. /*****************************************************************************/
  804.  
  805.  
  806.  
  807. /* The TextEdit control that was hit by calling FindControl is saved in a
  808. ** global variable, since the CDEF has no way of returning what kind it was.
  809. ** To determine that it was a TextEdit control that was hit, first call this
  810. ** function.  The first call returns the old value in the global variable,
  811. ** plus it resets the global to nil.  Then call FindControl(), and then
  812. ** call this function again.  If it returns nil, then a TextEdit control
  813. ** wasn't hit.  If it returns non-nil, then it was a TextEdit control that
  814. ** was hit, and specifically the one returned. */
  815.  
  816. #pragma segment Controls
  817. ControlHandle    CTECtlHit(void)
  818. {
  819.     ControlHandle    ctl;
  820.  
  821.     ctl = gFoundViewCtl;
  822.     gFoundViewCtl = nil;
  823.     return(ctl);
  824. }
  825.  
  826.  
  827.  
  828. static ControlHandle    dummyCTECtlHit(void)
  829. {
  830.     return(nil);
  831. }
  832.  
  833.  
  834.  
  835. /*****************************************************************************/
  836.  
  837.  
  838.  
  839. /* Disposes of the TERecord, TextEdit control, and any related scrollbars. */
  840.  
  841. #pragma segment Controls
  842. void    CTEDispose(TEHandle teHndl)
  843. {
  844.     WindowPtr    oldPort;
  845.  
  846.     if (teHndl) {
  847.         GetPort(&oldPort);
  848.         SetPort((*teHndl)->inPort);
  849.         TEDispose(CTEDisposeView(CTEViewFromTE(teHndl)));
  850.             /* Dispose of the TextEdit control completely.  This includes
  851.             ** scrollbars, as well as the TextEdit view control.
  852.             */
  853.         SetPort(oldPort);
  854.     }
  855. }
  856.  
  857.  
  858.  
  859. /*****************************************************************************/
  860.  
  861.  
  862.  
  863. /* Dispose of the view control and related scrollbars.  This function also
  864. ** returns the handle to the TextEdit record, since it was just orphaned.
  865. ** Use this function if you want to get rid of a TextEdit control, but you
  866. ** want to keep the TextEdit record. */
  867.  
  868. #pragma segment Controls
  869. TEHandle    CTEDisposeView(ControlHandle viewCtl)
  870. {
  871.     TEHandle        te;
  872.     short            vert;
  873.     ControlHandle    ctl;
  874.  
  875.     if (!viewCtl) return(nil);
  876.  
  877.     te = (TEHandle)GetCRefCon(viewCtl);
  878.     SetCRefCon(viewCtl, (long)nil);
  879.  
  880.     for (vert = 0; vert < 2; ++vert)
  881.         if (ctl = CTEScrollFromView(viewCtl, vert))
  882.             DisposeControl(ctl);
  883.  
  884.     DisposeControl(viewCtl);
  885.     return(te);
  886. }
  887.  
  888.  
  889.  
  890. /*****************************************************************************/
  891.  
  892.  
  893.  
  894. /* Returns the full document height. */
  895.  
  896. #pragma segment Controls
  897. short    CTEDocHeight(TEHandle teHndl)
  898. {
  899.     short        h, lh, a;
  900.     TextStyle    st;
  901.  
  902.     if (!teHndl) return(0);
  903.  
  904.     h = TEGetHeight((*teHndl)->nLines, 1, teHndl);
  905.         /* Get the whole doc height. */
  906.  
  907.     if ((*teHndl)->nLines != CTENumTextLines(teHndl)) {        /* If # of lines is wrong by one... */
  908.         TEGetStyle((*teHndl)->teLength, &st, &lh, &a, teHndl);
  909.         h += lh;                                            /* Add in height of last line. */
  910.     }
  911.  
  912.     return(h);
  913. }
  914.  
  915.  
  916.  
  917. /*****************************************************************************/
  918.  
  919.  
  920.  
  921. /* Enable or disable edit menu items based on the active TextEdit control.
  922. ** You pass the menu ID of the undo item in undoID, and the menu ID of the
  923. ** cut item in cutID.  If undoID or cutID is non-zero, then some action is
  924. ** performed.  If you pass a non-zero value for cutID, then the other menu
  925. ** items cut/copy/paste/clear are updated to reflect the status of the
  926. ** active TextEdit control. */
  927.  
  928. #pragma segment Controls
  929. Boolean    CTEEditMenu(Boolean *activeItem, short editMenu, short undoID, short cutID)
  930. {
  931.     TEHandle        te;
  932.     MenuHandle        menu;
  933.     Boolean            active;
  934.     ControlHandle    viewCtl;
  935.     CTEDataHndl        teData;
  936.     long            dataLen, scrapOffset;
  937.  
  938.     active = false;
  939.     if (activeItem) *activeItem = false;
  940.     menu = GetMHandle(editMenu);
  941.  
  942.     if (undoID)
  943.         DisableItem(menu, undoID);
  944.  
  945.     if (cutID) {
  946.         DisableItem(menu, cutID);            /* Disable cut. */
  947.         DisableItem(menu, cutID + 1);        /* Disable copy. */
  948.         DisableItem(menu, cutID + 2);        /* Disable paste. */
  949.         DisableItem(menu, cutID + 3);        /* Disable clear. */
  950.     }
  951.  
  952.     if (!(te = gActiveTEHndl)) return(false);
  953.  
  954.     if (undoID) {
  955.         if (viewCtl = CTEViewFromTE(te)) {
  956.             teData = (CTEDataHndl)(*viewCtl)->contrlData;
  957.             if ((*teData)->undoText) {
  958.                 EnableItem(menu, undoID);
  959.                 active = true;
  960.             }
  961.         }
  962.     }
  963.  
  964.     if (cutID) {
  965.         if ((*te)->selStart != (*te)->selEnd) {
  966.             if (!CTEReadOnly(te)) {
  967.                 EnableItem(menu, cutID);        /* Enable cut. */
  968.                 EnableItem(menu, cutID + 3);    /* Enable clear. */
  969.             }
  970.             active = true;
  971.             EnableItem(menu, cutID + 1);        /* Enable copy. */
  972.         }
  973.         if (!CTEReadOnly(te)) {
  974.             dataLen = GetScrap(nil, 'TEXT', &scrapOffset);
  975.             if (dataLen > 0) {
  976.                 active = true;
  977.                 EnableItem(menu, cutID + 2);        /* Enable paste. */
  978.             }
  979.         }
  980.     }
  981.  
  982.     if (activeItem)
  983.         *activeItem = active;
  984.  
  985.     return(true);
  986. }
  987.  
  988.  
  989.  
  990. /*****************************************************************************/
  991.  
  992.  
  993.  
  994. /* Handle the event if it applies to the active TextEdit control.  If some
  995. ** action occured due to the event, return true. */
  996.  
  997. #pragma segment Controls
  998. Boolean    CTEEvent(WindowPtr window, EventRecord *event, short *action)
  999. {
  1000.     WindowPtr    clickWindow;
  1001.     short        actn;
  1002.  
  1003.     if (action)
  1004.         *action = 0;
  1005.  
  1006.     switch(event->what) {
  1007.  
  1008.         case mouseDown:
  1009.             if (FindWindow(event->where, &clickWindow) == inContent)
  1010.                 if (window == clickWindow)
  1011.                     if (((WindowPeek)window)->hilited) return(CTEClick(window, event, action));
  1012.             break;
  1013.  
  1014.         case autoKey:
  1015.         case keyDown:
  1016.             if (!(event->modifiers & cmdKey)) {
  1017.                 actn = CTEKey(window, event);
  1018.                 if (action)
  1019.                     *action = actn;
  1020.                 if (actn) return(true);
  1021.             }
  1022.             break;
  1023.     }
  1024.  
  1025.     return(false);
  1026. }
  1027.  
  1028.  
  1029.  
  1030. /*****************************************************************************/
  1031.  
  1032.  
  1033.  
  1034. /* Returns the active TextEdit control, if any.  If nil is passed in, then
  1035. ** the return value represents whatever TextEdit control is active, independent
  1036. ** of what window it is in.  If a window is passed in, then it returns a
  1037. ** TextEdit control only if the active control is in the specified window.
  1038. ** If the active TextEdit control is in some other window, then nil is returned. */
  1039.  
  1040. #pragma segment Controls
  1041. TEHandle    CTEFindActive(WindowPtr window)
  1042. {
  1043.     if (!window) return(gActiveTEHndl);
  1044.         /* User wants whatever is active one, for whatever window. */
  1045.  
  1046.     if (!gActiveTEHndl) return(nil);
  1047.     if (window != (*gActiveTEHndl)->inPort) return(nil);
  1048.     return(gActiveTEHndl);
  1049. }
  1050.  
  1051.  
  1052.  
  1053. static TEHandle    dummyCTEFindActive(WindowPtr window)
  1054. {
  1055. #pragma unused (window)
  1056.     return(nil);
  1057. }
  1058.  
  1059.  
  1060.  
  1061. /*****************************************************************************/
  1062.  
  1063.  
  1064.  
  1065. /* This determines if a TextEdit control was clicked on directly.  This does
  1066. ** not determine if a related scrollbar was clicked on.  If a TextEdit
  1067. ** control was clicked on, then true is returned, as well as the TextEdit
  1068. ** handle and the handle to the view control. */
  1069.  
  1070. #pragma segment Controls
  1071. ControlHandle    CTEFindCtl(WindowPtr window, EventRecord *event, TEHandle *teHndl, ControlHandle *ctlHit)
  1072. {
  1073.     WindowPtr        oldPort;
  1074.     Point            mouseLoc;
  1075.     ControlHandle    ctl, tectl;
  1076.     TEHandle        te;
  1077.  
  1078.     if (ctlHit) *ctlHit = nil;
  1079.     if (teHndl) *teHndl = nil;
  1080.  
  1081.     gFoundTEHndl = nil;
  1082.  
  1083.     if (window) {
  1084.         GetPort(&oldPort);
  1085.         SetPort(window);
  1086.         mouseLoc = event->where;
  1087.         GlobalToLocal(&mouseLoc);
  1088.         SetPort(oldPort);
  1089.  
  1090.         if (!WhichControl(mouseLoc, 0, window, &ctl)) return(nil);
  1091.             /* Didn't hit a thing, so forget it. */
  1092.  
  1093.         if (te = CTEFromScroll(ctl, &tectl)) {
  1094.             if (ctlHit)
  1095.                 *ctlHit = ctl;
  1096.             if ((*tectl)->contrlHilite == 255) return(nil);
  1097.             if (teHndl)
  1098.                 *teHndl = te;
  1099.             return(tectl);
  1100.         }
  1101.  
  1102.         FindControl(mouseLoc, window, &ctl);
  1103.         if (*(*ctl)->contrlDefProc != *(Handle)gCDEF) return(nil);
  1104.             /* Control hit was above TE control, so we didn't hit a TE control. */
  1105.         if (ctlHit)
  1106.             *ctlHit = ctl;
  1107.         if (teHndl)
  1108.             *teHndl = gFoundTEHndl;
  1109.         if (gFoundTEHndl) return(ctl);
  1110.     }
  1111.  
  1112.     return(nil);
  1113. }
  1114.  
  1115.  
  1116.  
  1117. /*****************************************************************************/
  1118.  
  1119.  
  1120.  
  1121. /* Find the TextEdit record that is related to the indicated scrollbar. */
  1122.  
  1123. #pragma segment Controls
  1124. TEHandle    CTEFromScroll(ControlHandle scrollCtl, ControlHandle *retCtl)
  1125. {
  1126.     WindowPtr        window;
  1127.     ControlHandle    viewCtl;
  1128.     TEHandle        te;
  1129.  
  1130.     if (!IsScrollBar(scrollCtl)) {
  1131.         if (retCtl) *retCtl = nil;
  1132.         return(nil);
  1133.     }
  1134.  
  1135.     window = (*scrollCtl)->contrlOwner;
  1136.  
  1137.     for (viewCtl = nil;;) {
  1138.         viewCtl = CTENext(window, &te, viewCtl, 1, false);
  1139.         if (!viewCtl) return(nil);
  1140.         if (viewCtl == (ControlHandle)GetCRefCon(scrollCtl)) {
  1141.             if (retCtl) *retCtl = viewCtl;
  1142.             return(te);
  1143.         }
  1144.     }
  1145. }
  1146.  
  1147.  
  1148.  
  1149. /*****************************************************************************/
  1150.  
  1151.  
  1152.  
  1153. /* Hide the designated TextEdit control and related scrollbars. */
  1154.  
  1155. #pragma segment Controls
  1156. void    CTEHide(TEHandle teHndl)
  1157. {
  1158.     ControlHandle    viewCtl, scrollCtl;
  1159.     short            i;
  1160.  
  1161.     if (teHndl) {
  1162.         CTEActivate(false, teHndl);
  1163.         viewCtl = CTEViewFromTE(teHndl);
  1164.         if (viewCtl) {
  1165.             HideControl(viewCtl);
  1166.             for (i = 0; i < 2; i++) {
  1167.                 scrollCtl = CTEScrollFromView(viewCtl, i);
  1168.                 if (scrollCtl)
  1169.                     HideControl(scrollCtl);
  1170.             }
  1171.         }
  1172.     }
  1173. }
  1174.  
  1175.  
  1176.  
  1177. /*****************************************************************************/
  1178.  
  1179.  
  1180.  
  1181. /* Blink the caret in the active TextEdit control.  The active TextEdit
  1182. ** control may be read-only, in which case the caret does not blink. */
  1183.  
  1184. #pragma segment Controls
  1185. void    CTEIdle(void)
  1186. {
  1187.     WindowPtr    window;
  1188.  
  1189.     if (gActiveTEHndl) {
  1190.         window = (*gActiveTEHndl)->inPort;
  1191.         if (((WindowPeek)window)->hilited)
  1192.             if (GetWRefCon(window))
  1193.                 TEIdle(gActiveTEHndl);
  1194.     }
  1195. }
  1196.  
  1197.  
  1198.  
  1199. /*****************************************************************************/
  1200.  
  1201.  
  1202.  
  1203. /* See if the keypress event applies to the TextEdit control, and if it does,
  1204. ** handle it and return non-zero.  If the key caused a change in the TERecord,
  1205. ** return 2.  If the key was handled with no change to the TERecord, return 1. */
  1206.  
  1207. #pragma segment Controls
  1208. short    CTEKey(WindowPtr window, EventRecord *event)
  1209. {
  1210.     TEHandle            te;
  1211.     ControlHandle        viewCtl;
  1212.     short                maxTextLen, selStart, selEnd;
  1213.     short                textSelected, arrowKey, retval;
  1214.     char                key;
  1215.     CTEDataHndl            teData;
  1216.     CTEKeyFilterProcPtr    kproc;
  1217.     CTEFastKeysProcPtr    fproc;
  1218.     short                handled;
  1219.     Boolean                looping, refig, tsmHandled;
  1220.     EventRecord            lclEvent;
  1221.  
  1222.     if (!(te = gActiveTEHndl))                return(0);
  1223.     if ((*gActiveTEHndl)->inPort != window) return(0);
  1224.     if (CTEReadOnly(te))                    return(0);
  1225.     if (!(viewCtl = CTEViewFromTE(te)))        return(0);
  1226.  
  1227.     teData = (CTEDataHndl)(*viewCtl)->contrlData;
  1228.     if (kproc = (*teData)->keyFilter)
  1229.         if ((*kproc)(te, event, &handled))
  1230.             return(handled);
  1231.  
  1232.     retval = 1;
  1233.  
  1234.     for (refig = looping = false;; looping = true) {
  1235.  
  1236.         if (looping) {
  1237.  
  1238.             if (!(fproc = (*teData)->fastKeys)) {
  1239.                 if (refig) {
  1240.                     CTEAdjustTEBottom(te);
  1241.                     CTEAdjustScrollValues(te);
  1242.                 }
  1243.                 return(retval);
  1244.             }                        /* No fast keys looping proc defined, so leave. */
  1245.  
  1246.             if ((gSystemVersion >= 0x0700) && (HMGetBalloons())) {
  1247.                 if (refig) {
  1248.                     CTEAdjustTEBottom(te);
  1249.                     CTEAdjustScrollValues(te);
  1250.                 }
  1251.                 return(retval);
  1252.             }                        /* Don't call OSEventAvail if balloons are active, since
  1253.                                     ** the help manager may move a balloon when it is called.
  1254.                                     ** If we moved balloons now, that might be bad, since it
  1255.                                     ** is likely that BeginContent was called prior to getting
  1256.                                     ** here.  BeginContent calls BeginUpdate, which munges the
  1257.                                     ** visRgn.  If balloons were managed, then visRgns would get
  1258.                                     ** recalculated, and this would mess up our munged balloon. */
  1259.  
  1260.             event = &lclEvent;
  1261.             if (!OSEventAvail((mDownMask | keyDownMask | autoKeyMask), event)) {
  1262.                 if (refig) {
  1263.                     CTEAdjustTEBottom(te);
  1264.                     CTEAdjustScrollValues(te);
  1265.                 }
  1266.                 return(retval);
  1267.             }
  1268.  
  1269.             if (kproc) {
  1270.                 if ((*kproc)(te, event, &handled)) {
  1271.                     if (refig) {
  1272.                         CTEAdjustTEBottom(te);
  1273.                         CTEAdjustScrollValues(te);
  1274.                     }
  1275.                     return(retval);
  1276.                 }
  1277.             }
  1278.  
  1279.             if (!(*fproc)(te, event)) {
  1280.                 if (refig) {
  1281.                     CTEAdjustTEBottom(te);
  1282.                     CTEAdjustScrollValues(te);
  1283.                 }
  1284.                 return(retval);
  1285.             }
  1286.  
  1287.             GetOSEvent((mDownMask | keyDownMask | autoKeyMask), event);
  1288.         }
  1289.  
  1290.         maxTextLen = (*teData)->maxTextLen;
  1291.         key        = event->message & charCodeMask;
  1292.         selStart   = (*te)->selStart;
  1293.         selEnd     = (*te)->selEnd;
  1294.         if (selStart > selEnd) {
  1295.             selStart = selEnd;
  1296.             selEnd = (*te)->selStart;
  1297.         }
  1298.         textSelected = (selStart != selEnd);
  1299.         arrowKey     = ((key >= chLeft) && (key <= chDown));
  1300.  
  1301.         if (
  1302.             (textSelected) ||                /* If selection range to be replaced or */
  1303.             (arrowKey) ||                    /* key is an arrow or                    */
  1304.             (key == 8) ||                    /* key is a delete or                   */
  1305.             ((*te)->teLength < maxTextLen)    /* we have space for the key...         */
  1306.         ) {
  1307.             tsmHandled = false;
  1308.             if (TSMTEAvailable()) {
  1309.                 if (tsmHandled = TSMEvent(event))
  1310.                     if (!arrowKey)
  1311.                         retval = 2;
  1312.             }
  1313.             if (!tsmHandled) {
  1314.                 if (arrowKey) {
  1315.                     (*teData)->newUndo = true;
  1316.                     TEKey(key, te);
  1317.                 }
  1318.                 else {
  1319.                     CTENewUndo(viewCtl, false);        /* Process the character... */
  1320.                     TEKey(key, te);
  1321.                     retval = 2;
  1322.                 }
  1323.             }
  1324.             refig = true;
  1325.         }
  1326.     }
  1327. }
  1328.  
  1329.  
  1330.  
  1331. static short    dummyCTEKey(WindowPtr window, EventRecord *event)
  1332. {
  1333. #pragma unused (window, event)
  1334.     return(0);
  1335. }
  1336.  
  1337.  
  1338.  
  1339. /*****************************************************************************/
  1340.  
  1341.  
  1342.  
  1343. /* This function is used to move a TextEdit control.  Pass it the TextEdit
  1344. ** record to move, plus the new position.  It will move the TextEdit control,
  1345. ** along with any scrollbars the control may have.  All areas that need
  1346. ** updating are cleared and invalidated. */
  1347.  
  1348. #pragma segment Controls
  1349. void    CTEMove(TEHandle teHndl, short newH, short newV)
  1350. {
  1351.     WindowPtr        oldPort;
  1352.     Rect            viewRct, brdrRct, rct;
  1353.     short            i, dx, dy, mode;
  1354.     Boolean            hScroll, vScroll;
  1355.     ControlHandle    viewCtl, ctl;
  1356.     CTEDataHndl        teData;
  1357.  
  1358.     if (!(viewCtl = CTEViewFromTE(teHndl))) return;
  1359.  
  1360.     GetPort(&oldPort);
  1361.     SetPort((*teHndl)->inPort);
  1362.  
  1363.     viewRct = (*viewCtl)->contrlRect;
  1364.     teData  = (CTEDataHndl)(*viewCtl)->contrlData;
  1365.     brdrRct = (*teData)->brdrRect;
  1366.     mode    = (*teData)->mode;
  1367.  
  1368.     dx = newH - viewRct.left;
  1369.     dy = newV - viewRct.top;
  1370.     if ((!dx) && (!dy)) return;
  1371.  
  1372.     hScroll = (mode & cteHScroll) ? 1 : 0;
  1373.     vScroll = (mode & cteVScroll) ? 1 : 0;
  1374.         /* Get enough info to determine if we need to fix the scrollbar sizes. */
  1375.  
  1376.     rct = viewRct;                /* Erase all of the old list control and scrollbars. */
  1377.     if (!EmptyRect(&brdrRct))
  1378.         UnionRect(&rct, &brdrRct, &rct);
  1379.     if (vScroll)
  1380.         rct.right += 15;
  1381.     if (hScroll)
  1382.         rct.bottom += 15;
  1383.     if (mode & cteShowActive)
  1384.         InsetRect(&rct, -4, -4);
  1385.     EraseRect(&rct);
  1386.     InvalRect(&rct);
  1387.  
  1388.     for (i = 0; i < 2; ++i) {
  1389.         if (ctl = CTEScrollFromView(viewCtl, i)) {
  1390.             rct = (*ctl)->contrlRect;
  1391.             MoveControl(ctl, rct.left + dx, rct.top + dy);
  1392.         }
  1393.     }
  1394.  
  1395.     OffsetRect(&viewRct, dx, dy);
  1396.     InvalRect(&viewRct);
  1397.     OffsetRect(&brdrRct, dx, dy);
  1398.     InvalRect(&brdrRct);
  1399.  
  1400.     (*viewCtl)->contrlRect = viewRct;
  1401.     (*teData)->brdrRect    = brdrRct;
  1402.  
  1403.     OffsetRect(&(*teHndl)->destRect, dx, dy);
  1404.     OffsetRect(&(*teHndl)->viewRect, dx, dy);
  1405.  
  1406.     viewRct.top  = viewRct.bottom - 16;
  1407.     viewRct.left = viewRct.right  - 16;
  1408.  
  1409.     if (mode & (cteVScrollLessGrow - cteVScroll + cteHScrollLessGrow - cteHScroll))
  1410.         EraseRect(&viewRct);
  1411.  
  1412.     if (mode & (cteVScrollLessGrow - cteVScroll))
  1413.         OffsetRect(&viewRct, 16, 0);
  1414.     if (mode & (cteHScrollLessGrow - cteHScroll))
  1415.         OffsetRect(&viewRct, 0, 16);
  1416.     InvalRect(&viewRct);
  1417.  
  1418.     SetPort(oldPort);
  1419. }
  1420.  
  1421.  
  1422.  
  1423. /*****************************************************************************/
  1424.  
  1425.  
  1426.  
  1427. /* Create a new TextEdit control.  See the comments at the beginning of this
  1428. ** file for more information.  Note that this function doesn't get a dummy,
  1429. ** as you really mean to have this code if you call it.  It calls CTEInitialize(),
  1430. ** just to make sure that the proc pointers are set to point to the real function,
  1431. ** instead of the dummy functions.  By having CLTENew() call CTEInitialize(), the
  1432. ** application won't have to worry about calling CTEInitialize().  By the time that
  1433. ** the application is using a TextEdit control, the proc pointers will be initialized. */
  1434.  
  1435. #pragma segment Controls
  1436. OSErr    CTENew(short viewID, Boolean vis, WindowPtr window, TEHandle *teHndl, Rect *cRect, Rect *dRect,
  1437.                Rect *vRect, Rect *bRect, short maxTextLen, short mode)
  1438. {
  1439.     Rect            ctlRct, destRct, viewRct, brdrRct, scrollRct;
  1440.     WindowPtr        oldPort;
  1441.     TEHandle        te;
  1442.     short            width, height, lh, a;
  1443.     ControlHandle    viewCtl, hScrollCtl, vScrollCtl;
  1444.     CTEDataHndl        teData;
  1445.     TextStyle        styl;
  1446.     OSErr            err;
  1447.     TSMDocumentID    docID;
  1448.     TSMTERecHandle    tsmRec;
  1449.     static OSType    supportedInterfaceTypes[] = {kTSMTEInterfaceType};
  1450.  
  1451.     CTEInitialize();
  1452.  
  1453.     if (teHndl)
  1454.         *teHndl = nil;            /* Assume that we will fail. */
  1455.  
  1456.     GetPort(&oldPort);
  1457.     SetPort(window);
  1458.  
  1459.     ctlRct  = *cRect;
  1460.     destRct = *dRect;
  1461.     viewRct = *vRect;
  1462.     brdrRct = *bRect;
  1463.         /* Make sure that the rects are not in memory that may move. */
  1464.  
  1465.     te = TEStylNew(&destRct, &viewRct);            /* Do the main thing. */
  1466.  
  1467.     err = noErr;
  1468.     viewCtl = hScrollCtl = vScrollCtl = nil;
  1469.         /* Prepare for various failures. */
  1470.  
  1471.     docID = nil;
  1472.  
  1473.     if (te) {        /* If we were able to create the TextEdit record... */
  1474.  
  1475.         if(TSMTEAvailable()) {
  1476.             if (!(err = NewTSMDocument(1, supportedInterfaceTypes, &docID, (long)&tsmRec))) {
  1477.                 (*tsmRec)->textH           = te;
  1478.                 (*tsmRec)->preUpdateProc  = nil;
  1479.                 (*tsmRec)->postUpdateProc = (TSMPostUpdateProcPtr)&TSMTEUpdateProc;
  1480.                 (*tsmRec)->updateFlag     = 0;
  1481.                 (*tsmRec)->refCon         = 0;
  1482.             }
  1483.         }
  1484.  
  1485.         if (mode & cteCenterJustify)
  1486.             TESetJust(1, te);
  1487.  
  1488.         if (mode & cteRightJustify)
  1489.             TESetJust(-1, te);
  1490.  
  1491.         TEGetStyle(0, &styl, &lh, &a, te);
  1492.         if (styl.tsFont == 1) {
  1493.             styl.tsFont = GetAppFont();
  1494.             styl.tsSize = GetDefFontSize();
  1495.             TESetStyle((doFont | doSize), &styl, false, te);
  1496.         }
  1497.  
  1498.         TEAutoView(true, te);
  1499.             /* Let TextEdit 3.0 do most of the scrolling work. */
  1500.         gDefaultClikLoop = (*te)->clikLoop;
  1501.         (*te)->clikLoop  = ASMTECLIKLOOP;
  1502.             /* We will do the remainder of the work. */
  1503.  
  1504.         if (!gCDEF) {
  1505.             gCDEF = (cdefRsrcJMPHndl)GetResource('CDEF', (viewID / 16));
  1506.             if (!gCDEF) return(resNotFound);
  1507.             (*gCDEF)->jmpAddress = (long)CTECtl;
  1508.             FlushInstructionCache();    /* Make sure that instruction caches don't kill us. */
  1509.         }
  1510.  
  1511.         if (mode & cteHScroll) {        /* Caller wants a horizontal scrollbar... */
  1512.             SetRect(&scrollRct, 0, 0, 100, 16);
  1513.             hScrollCtl = NewControl(window, &scrollRct, "\p", true, 0, 0, 0, scrollBarProc, 0);
  1514.             if (hScrollCtl) {
  1515.                 MoveControl(hScrollCtl, brdrRct.left, brdrRct.bottom - 1);
  1516.                 width = brdrRct.right - brdrRct.left;
  1517.                 if (mode & (cteHScrollLessGrow - cteHScroll))
  1518.                     if (!(mode & cteVScroll))
  1519.                         width -= 15;
  1520.                 SizeControl(hScrollCtl, width, 16);
  1521.                     /* Line the scrollbar up with the borderRct. */
  1522.             }
  1523.             else err = resNotFound;
  1524.         }
  1525.  
  1526.         if (mode & cteVScroll) {        /* Caller wants a vertical scrollbar... */
  1527.             SetRect(&scrollRct, 0, 0, 16, 100);
  1528.             vScrollCtl = NewControl(window, &scrollRct, "\p", true, 0, 0, 0, scrollBarProc, 0);
  1529.             if (vScrollCtl) {
  1530.                 MoveControl(vScrollCtl, brdrRct.right - 1, brdrRct.top);
  1531.                 height = brdrRct.bottom - brdrRct.top;
  1532.                 if (mode & (cteVScrollLessGrow - cteVScroll))
  1533.                     if (!(mode & cteHScroll))
  1534.                         height -= 15;
  1535.                 SizeControl(vScrollCtl, 16, height);
  1536.                     /* Line the scrollbar up with the borderRct. */
  1537.             }
  1538.             else err = resNotFound;
  1539.         }
  1540.  
  1541.         viewCtl = NewControl(window, &ctlRct, "\p", false, 0, 0, 0, viewID, (long)te);
  1542.             /* Use our custom view cdef.  It's wierd, but it's small. */
  1543.  
  1544.         if (!viewCtl)
  1545.             err = resNotFound;
  1546.         else {
  1547.             if (hScrollCtl)
  1548.                 SetCRefCon(hScrollCtl, (long)viewCtl);
  1549.             if (vScrollCtl)
  1550.                 SetCRefCon(vScrollCtl, (long)viewCtl);
  1551.             (*viewCtl)->contrlData = nil;
  1552.             if (teData = (CTEDataHndl)NewHandleClear(sizeof(CTEDataRec))) {
  1553.                 (*teData)->maxTextLen   = maxTextLen;
  1554.                 (*teData)->newUndo      = true;
  1555.                 (*teData)->mode         = mode;
  1556.                 (*teData)->brdrRect     = brdrRct;
  1557.                 (*teData)->brdrRect     = brdrRct;
  1558.                 if (!(mode & cteNoFastKeys))
  1559.                     (*teData)->fastKeys = GoFast;
  1560.                 (*teData)->docID        = docID;
  1561.                 (*viewCtl)->contrlData  = (Handle)teData;
  1562.             }
  1563.             else {
  1564.                 err = memFullErr;
  1565.                 if (docID)
  1566.                     DeleteTSMDocument(docID);
  1567.             }
  1568.         }
  1569.     }
  1570.     else err = memFullErr;
  1571.  
  1572.     SetPort(oldPort);
  1573.  
  1574.     if (err) {        /* Oops.  Somebody wasn't happy. */
  1575.         if (viewCtl)
  1576.             DisposeControl(viewCtl);
  1577.                 /* This also disposes of TextEdit handle! */
  1578.         else
  1579.             if (te)
  1580.                 TEDispose(te);
  1581.                     /* We have to dispose of the TextEdit handle ourselves if
  1582.                     ** creating the view control failed. */
  1583.  
  1584.         te = nil;        /* Indicate that there is no TextEdit control. */
  1585.  
  1586.         if (hScrollCtl)
  1587.             DisposeControl(hScrollCtl);
  1588.                 /* More clean-up. */
  1589.  
  1590.         if (vScrollCtl)
  1591.             DisposeControl(vScrollCtl);
  1592.                 /* And still more clean-up. */
  1593.     }
  1594.     else {
  1595.         if (mode & cteActive) CTEWindActivate(window, false);
  1596.  
  1597.         if (vis) ShowControl(viewCtl);        /* Since everything worked, show the control. */
  1598.  
  1599.         if (mode & cteReadOnly)
  1600.             (*te)->caretHook = (ProcPtr)ASMNOCARET;
  1601.                 /* If read-only, then disable caret for this
  1602.                 ** TextEdit control. */
  1603.  
  1604.         if (teHndl)
  1605.             *teHndl = te;
  1606.         CTEAdjustScrollValues(te);
  1607.             /* Give the scrollbars an initial value.  This is because the
  1608.             ** TextEdit control could have been created with
  1609.             ** destRct.top < viewRct.top or destRct.left < viewRct.left.
  1610.             ** I don't know why anyone would want to, but it is legal.
  1611.             */
  1612.     }
  1613.  
  1614.     return(err);
  1615. }
  1616.  
  1617. static Boolean    GoFast(TEHandle teHndl, EventRecord *event)
  1618. {
  1619. #pragma unused (teHndl)
  1620.  
  1621.     char    key;
  1622.  
  1623.     if (event->modifiers & cmdKey) return(false);
  1624.     key = event->message & charCodeMask;
  1625.     if (key == chTab) return(false);
  1626.  
  1627.     return(true);
  1628. }
  1629.  
  1630.  
  1631.  
  1632. /*****************************************************************************/
  1633.  
  1634.  
  1635.  
  1636. /* Save the data (if appropriate) so that user can undo. */
  1637.  
  1638. #pragma segment Controls
  1639. void    CTENewUndo(ControlHandle viewCtl, Boolean alwaysNewUndo)
  1640. {
  1641.     TEHandle        teHndl;
  1642.     CTEDataHndl        teData;
  1643.     Handle            hText, uText;
  1644.     StScrpHandle    hStyl, uStyl;
  1645.  
  1646.     if (!viewCtl) return;
  1647.  
  1648.     teHndl = (TEHandle)GetCRefCon(viewCtl);
  1649.     teData = (CTEDataHndl)(*viewCtl)->contrlData;
  1650.  
  1651.     if ((!alwaysNewUndo) && (!(*teData)->newUndo)) return;
  1652.         /* If there isn't a new undo to record, then just return. */
  1653.  
  1654.     hText = (*teHndl)->hText;
  1655.     hStyl = CTEGetFullStylScrap(teHndl);
  1656.     uText = (*teData)->undoText;
  1657.     uStyl = (*teData)->undoStyl;
  1658.         /* hText/hStyl is styled text in the TextEdit record that is being edited.  */
  1659.         /* uText/uStyl is the undo data (if any) prior to current edit task. */
  1660.  
  1661.     if (uText) {                /* Dump the old undo info, if any. */
  1662.         DisposeHandle(uText);
  1663.         (*teData)->undoText = nil;
  1664.     }
  1665.     if (uStyl) {
  1666.         DisposeHandle((Handle)uStyl);
  1667.         (*teData)->undoStyl = nil;
  1668.     }
  1669.  
  1670.     if (!hStyl) return;
  1671.         /* Not enough ram to record undo, so leave. */
  1672.  
  1673.     uText = hText;
  1674.     if (HandToHand(&uText)) {            /* Copy the text (if ram available). */
  1675.         DisposeHandle((Handle)hStyl);
  1676.         return;                            /* Not enough ram to record undo. */
  1677.     }
  1678.  
  1679.     (*teData)->undoText     = uText;
  1680.     (*teData)->undoStyl     = hStyl;
  1681.     (*teData)->newUndo      = false;
  1682.     (*teData)->undoSelStart = (*teHndl)->selStart;
  1683.     (*teData)->undoSelEnd   = (*teHndl)->selEnd;
  1684. }
  1685.  
  1686.  
  1687.  
  1688. /*****************************************************************************/
  1689.  
  1690.  
  1691.  
  1692. /* Get the next TextEdit control in the window.  You pass it a control handle
  1693. ** for the view control, or nil to start at the beginning of the window.
  1694. ** It returns both a TextEdit handle and the view control handle for that
  1695. ** TextEdit record.  If none is found, nil is returned.  This allows you to
  1696. ** repeatedly call this function and walk through all the TextEdit controls
  1697. ** in a window. */
  1698.  
  1699. #pragma segment Controls
  1700. ControlHandle    CTENext(WindowPtr window, TEHandle *teHndl, ControlHandle ctl, short dir, Boolean justActive)
  1701. {
  1702.     short            i;
  1703.     ControlHandle    nextCtl, priorCtl;
  1704.  
  1705.     if (teHndl)
  1706.         *teHndl = nil;
  1707.  
  1708.     if (!window) return(nil);
  1709.     if (!gCDEF)  return(nil);
  1710.  
  1711.     if (dir > 0) {
  1712.         if (!ctl)
  1713.             ctl = ((WindowPeek)window)->controlList;
  1714.         else
  1715.             ctl = (*ctl)->nextControl;
  1716.         while (ctl) {
  1717.             if ((!justActive) || ((*ctl)->contrlVis)) {
  1718.                 if ((!justActive) || ((*ctl)->contrlHilite != 255)) {
  1719.                     if (*(*ctl)->contrlDefProc == *(Handle)gCDEF) {
  1720.                             /* The handle may be locked, which means that the hi-bit
  1721.                             ** may be on, thus invalidating the compare.  Dereference the
  1722.                             ** handles to get rid of this possibility. */
  1723.                         if (teHndl)
  1724.                             *teHndl = (TEHandle)GetCRefCon(ctl);
  1725.                         return(ctl);
  1726.                     }
  1727.                 }
  1728.             }
  1729.             ctl = (*ctl)->nextControl;
  1730.         }
  1731.         return(ctl);
  1732.     }
  1733.  
  1734.     nextCtl = ((WindowPeek)window)->controlList;
  1735.     for (i = 1, priorCtl = nil; ;nextCtl = (*nextCtl)->nextControl, ++i) {
  1736.         if ((!nextCtl) || (nextCtl == ctl)) {
  1737.             if (priorCtl)
  1738.                 if (teHndl)
  1739.                     *teHndl = (TEHandle)GetCRefCon(priorCtl);
  1740.             return(priorCtl);
  1741.         }
  1742.         if ((!justActive) || ((*nextCtl)->contrlVis)) {
  1743.             if ((!justActive) || ((*nextCtl)->contrlHilite != 255)) {
  1744.                 if (*(*nextCtl)->contrlDefProc == *(Handle)gCDEF)
  1745.                     priorCtl = nextCtl;
  1746.             }
  1747.         }
  1748.     }
  1749. }
  1750.  
  1751.  
  1752.  
  1753. static ControlHandle    dummyCTENext(WindowPtr window, TEHandle *teHndl, ControlHandle ctl, short dir, Boolean justActive)
  1754. {
  1755. #pragma unused (window, ctl, dir, justActive)
  1756.     *teHndl = nil;
  1757.     return(nil);
  1758. }
  1759.  
  1760.  
  1761.  
  1762. /*****************************************************************************/
  1763.  
  1764.  
  1765.  
  1766. /* Return the number of lines of text.  This is because there is a bug in
  1767. ** TextEdit where the number of lines returned is incorrect if the text
  1768. ** ends with a c/r.  This function adjusts for this bug. */
  1769.  
  1770. #pragma segment Controls
  1771. short    CTENumTextLines(TEHandle teHndl)
  1772. {
  1773.     short    lines;
  1774.     char    *cptr;
  1775.  
  1776.     if (!teHndl) return(0);
  1777.  
  1778.     lines = (*teHndl)->nLines;
  1779.  
  1780.     cptr = *((*teHndl)->hText);        /* Pointer to first TextEdit character. */
  1781.     if (cptr[(*teHndl)->teLength - 1] == kCrChar)
  1782.         ++lines;
  1783.             /* Since nLines isn’t right if the last character is a return,
  1784.             ** check for that case and fix it. */
  1785.  
  1786.     return(lines);
  1787. }
  1788.  
  1789.  
  1790.  
  1791. /*****************************************************************************/
  1792.  
  1793.  
  1794.  
  1795. /* Use this function to print the contents of a TextEdit record.  Pass it a
  1796. ** TextEdit handle, a pointer to a text offset, and a pointer to a rect to
  1797. ** print the text in.  The offset should be initialized to what character
  1798. ** in the TextEdit record you wish to start printing at (most likely 0).
  1799. ** The print function prints as much text as will fit in the rect, and
  1800. ** then updates the offset to tell you what is the first character that didn't
  1801. ** print.  You can then call the print function again with another rect with
  1802. ** this new offset, and it will print the text starting at the new offset.
  1803. ** This method is very useful when a single TextEdit record is longer than a
  1804. ** single page, and you wish the text to break at the end of the page.
  1805. ** The bottom of rect is also updated, along with the offset.  The bottom edge
  1806. ** of the rect is changed to reflect the actual bottom of the text printed.
  1807. ** This is useful because the rect passed in didn't necessarily hold an
  1808. ** integer number of lines of text.  The bottom of the rect is adjusted so
  1809. ** it exactly holds complete lines of text.
  1810. ** It is also possible that the rect could hold substantially more lines of
  1811. ** text than there are remaining.  Again, in this situation, the bottom of
  1812. ** rect is adjusted so that the rect tightly bounds the text printed.
  1813. ** The remaining piece of information passed back is an indicator that the
  1814. ** text through the end of the TextEdit record was printed.  When the end
  1815. ** of the text is reached, the offset for the next text to be printed is
  1816. ** returned as -1.  This indicates that processing of the TextEdit record
  1817. ** is complete. */
  1818.  
  1819. #pragma segment Controls
  1820. OSErr    CTEPrint(TEHandle teHndl, short *teOffset, Rect *teRct)
  1821. {
  1822.     short            len, offset, numLines, rctHeight, selStart, selEnd, lnum, y, lh;
  1823.     Handle            hText;
  1824.     Rect            keepDestRct, keepViewRct;
  1825.     WindowPtr        tePort, printPort;
  1826.     StScrpHandle    fullStyl, partStyl;
  1827.  
  1828.     if (!teHndl) return(noErr);
  1829.  
  1830.     len = (*teHndl)->teLength;
  1831.     if ((offset = *teOffset) >= len) {
  1832.         *teOffset = -1;                    /* We are offset further than we have text. */
  1833.         teRct->bottom = teRct->top;        /* Empty rect, since no text to draw.  */
  1834.         return(noErr);                    /* Just say that we have no more text. */
  1835.     }
  1836.  
  1837.     if (!(hText = NewHandle(len - offset))) return(memFullErr);
  1838.         /* Can't make part-text handle, so leave. */
  1839.  
  1840.     BlockMove((*(*teHndl)->hText) + offset, *hText, len - offset);
  1841.         /* hText now holds all characters after offset. */
  1842.  
  1843.     fullStyl = CTEGetFullStylScrap(teHndl);
  1844.     if (!fullStyl) {
  1845.         DisposeHandle(hText);        /* Couldn't get styles, so clean up and leave. */
  1846.         return(memFullErr);
  1847.     }
  1848.  
  1849.     if (partStyl = fullStyl) {                /* This assignment tests for styled TE, and if there
  1850.                                             ** is none, then partStyl gets initialized to nil. */
  1851.  
  1852.         selStart = (*teHndl)->selStart;        /* Get all styles after the offset. */
  1853.         selEnd   = (*teHndl)->selEnd;
  1854.         (*teHndl)->selStart = offset;
  1855.         (*teHndl)->selEnd   = (*teHndl)->teLength;
  1856.         partStyl = GetStylScrap(teHndl);
  1857.         (*teHndl)->selStart = selStart;
  1858.         (*teHndl)->selEnd   = selEnd;
  1859.         if (!partStyl) {
  1860.             DisposeHandle((Handle)fullStyl);
  1861.             DisposeHandle(hText);
  1862.             return(memFullErr);
  1863.         }
  1864.     }
  1865.  
  1866.     keepDestRct = (*teHndl)->destRect;
  1867.     keepViewRct = (*teHndl)->viewRect;
  1868.     tePort = (*teHndl)->inPort;
  1869.         /* Cache some information from the TextEdit record. */
  1870.  
  1871.     GetPort(&printPort);
  1872.     (*teHndl)->inPort = printPort;
  1873.     (*teHndl)->destRect = (*teHndl)->viewRect = *teRct;
  1874.         /* Install the print rect into the TextEdit record so CTESwapText
  1875.         ** recalcs against the print text. */
  1876.  
  1877.     hText = CTESwapText(teHndl, hText, partStyl, false);
  1878.     DisposeHandle((Handle)partStyl);
  1879.         /* We now have all of the text starting at offset formatted correctly in the print rect. */
  1880.  
  1881.     rctHeight = teRct->bottom - teRct->top;
  1882.     numLines  = CTENumTextLines(teHndl);
  1883.     for (y = 0, lnum = 1; lnum <= numLines; ++lnum) {
  1884.         lh = CTEGetLineHeight(teHndl, lnum, nil);
  1885.         if (y + lh > rctHeight) break;
  1886.         y += lh;
  1887.     }
  1888.     teRct->bottom = teRct->top + y;
  1889.     
  1890.     (*teHndl)->destRect = (*teHndl)->viewRect = *teRct;
  1891.         /* We now have the minimum rectangle to hold as much of the text
  1892.         ** as would fit into the rectangle passed in.  The final calc on
  1893.         ** the rect prevents partial lines from being drawn. */
  1894.  
  1895.     TEUpdate(teRct, teHndl);        /* Draw this portion of the text. */
  1896.  
  1897.     if (--lnum >= numLines)
  1898.         *teOffset = -1;
  1899.             /* Printed the last of the text for this record. */
  1900.     else
  1901.         *teOffset = (*teHndl)->lineStarts[lnum] + offset;
  1902.             /* Offset to the first character not printed. */
  1903.  
  1904.     (*teHndl)->inPort   = tePort;
  1905.     (*teHndl)->destRect = keepDestRct;
  1906.     (*teHndl)->viewRect = keepViewRct;
  1907.  
  1908.     DisposeHandle(CTESwapText(teHndl, hText, fullStyl, false));
  1909.     DisposeHandle((Handle)fullStyl);
  1910.  
  1911.     return(noErr);            /* Everything worked. */
  1912. }
  1913.  
  1914.  
  1915.  
  1916. /*****************************************************************************/
  1917.  
  1918.  
  1919.  
  1920. /* Return if the TextEdit control is read/write (true) or read-only (false). */
  1921.  
  1922. #pragma segment Controls
  1923. Boolean    CTEReadOnly(TEHandle teHndl)
  1924. {
  1925.     if (!teHndl) return(false);
  1926.     if ((*teHndl)->caretHook == (ProcPtr)ASMNOCARET) return(true);
  1927.     return(false);
  1928. }
  1929.  
  1930.  
  1931.  
  1932. /*****************************************************************************/
  1933.  
  1934.  
  1935.  
  1936. /* Return the control handle for the TextEdit control's scrollbar, either
  1937. ** vertical or horizontal.  If the scrollbar doesn't, nil is returned. */
  1938.  
  1939. #pragma segment Controls
  1940. ControlHandle    CTEScrollFromTE(TEHandle teHndl, Boolean vertScroll)
  1941. {
  1942.     ControlHandle    viewCtl;
  1943.  
  1944.     if (!(viewCtl = CTEViewFromTE(teHndl))) return(nil);
  1945.  
  1946.     return(CTEScrollFromView(viewCtl, vertScroll));
  1947. }
  1948.  
  1949.  
  1950.  
  1951. /*****************************************************************************/
  1952.  
  1953.  
  1954.  
  1955. /* Return the control handle for the scrollbar related to the view control,
  1956. ** either horizontal or vertical.  If the scrollbar doesn't exist, return nil. */
  1957.  
  1958. #pragma segment Controls
  1959. ControlHandle    CTEScrollFromView(ControlHandle viewCtl, Boolean vertScroll)
  1960. {
  1961.     ControlHandle    ctl;
  1962.     WindowPtr        window;
  1963.     Boolean            vert;
  1964.  
  1965.     if (!viewCtl) return(nil);
  1966.  
  1967.     window = (*viewCtl)->contrlOwner;
  1968.     ctl    = ((WindowPeek)window)->controlList;
  1969.  
  1970.     for (; ctl;) {
  1971.         if ((ControlHandle)GetCRefCon(ctl) == viewCtl) {
  1972.             vert = false;
  1973.             if ((*ctl)->contrlRect.right == (*ctl)->contrlRect.left + 16)
  1974.                 vert = true;
  1975.             if (vert == vertScroll) return(ctl);
  1976.         }
  1977.         ctl = (*ctl)->nextControl;
  1978.     }
  1979.     return(nil);
  1980. }
  1981.  
  1982.  
  1983.  
  1984. /*****************************************************************************/
  1985.  
  1986.  
  1987.  
  1988. /* A TextEdit control can have an optional key filter, which is called whenever
  1989. ** CTEKey() is called.  If you pass in nil, then the filtering is turned off.
  1990. ** This allows individual TextEdit controls to handle their own filtering.
  1991. ** The filter procedure is of the form:
  1992. **     Boolean (*CTEKeyFilterProcPtr)(TEHandle teHndl, EventRecord *event, short *handled);
  1993. ** If true is returned, then CTEKey() is aborted, and the value in "handled" is
  1994. ** returned.  By having a separate abort value and return value, you can determine
  1995. ** if processing of the event should be continued or not, independent of whether
  1996. ** or not you aborted CTEKey(). */
  1997.  
  1998. #pragma segment Controls
  1999. void    CTESetKeyFilter(TEHandle teHndl, CTEKeyFilterProcPtr proc)
  2000. {
  2001.     ControlHandle    viewCtl;
  2002.     CTEDataHndl        teData;
  2003.  
  2004.     if (!(viewCtl = CTEViewFromTE(teHndl))) return;
  2005.     teData = (CTEDataHndl)(*viewCtl)->contrlData;
  2006.     (*teData)->keyFilter = proc;
  2007. }
  2008.  
  2009.  
  2010.  
  2011. /*****************************************************************************/
  2012.  
  2013.  
  2014.  
  2015. #pragma segment Controls
  2016. void    CTESetFastKeys(TEHandle teHndl, CTEFastKeysProcPtr proc)
  2017. {
  2018.     ControlHandle    viewCtl;
  2019.     CTEDataHndl        teData;
  2020.  
  2021.     if (!(viewCtl = CTEViewFromTE(teHndl))) return;
  2022.     teData = (CTEDataHndl)(*viewCtl)->contrlData;
  2023.     (*teData)->fastKeys = proc;
  2024. }
  2025.  
  2026.  
  2027.  
  2028. /*****************************************************************************/
  2029.  
  2030.  
  2031.  
  2032. /* Select a range of text.  TESetSelect can't be used alone because it doesn't
  2033. ** update the scrollbars.  This function calls TESetSelect, and then fixes up
  2034. ** the scrollbars. */
  2035.  
  2036. #pragma segment Controls
  2037. void    CTESetSelect(short start, short end, TEHandle teHndl)
  2038. {
  2039.     WindowPtr        oldPort;
  2040.     ControlHandle    viewCtl;
  2041.     CTEDataHndl        teData;
  2042.  
  2043.     if (teHndl) {
  2044.         GetPort(&oldPort);
  2045.         SetPort((*teHndl)->inPort);
  2046.         TESetSelect(start, end, teHndl);
  2047.         CTEAdjustScrollValues(teHndl);
  2048.         if (viewCtl = CTEViewFromTE(teHndl)) {
  2049.             teData = (CTEDataHndl)(*viewCtl)->contrlData;
  2050.             (*teData)->newUndo = true;
  2051.         }
  2052.         SetPort(oldPort);
  2053.     }
  2054. }
  2055.  
  2056.  
  2057.  
  2058. static void    dummyCTESetSelect(short start, short end, TEHandle teHndl)
  2059. {
  2060. #pragma unused (start, end, teHndl)
  2061. }
  2062.  
  2063.  
  2064.  
  2065. /*****************************************************************************/
  2066.  
  2067.  
  2068.  
  2069. /* Show the designated TextEdit control and related scrollbars. */
  2070.  
  2071. #pragma segment Controls
  2072. void    CTEShow(TEHandle teHndl)
  2073. {
  2074.     ControlHandle    viewCtl, scrollCtl;
  2075.     short            i;
  2076.  
  2077.     if (viewCtl = CTEViewFromTE(teHndl)) {
  2078.         ShowControl(viewCtl);
  2079.         for (i = 0; i < 2; i++) {
  2080.             scrollCtl = CTEScrollFromView(viewCtl, i);
  2081.             if (scrollCtl)
  2082.                 ShowControl(scrollCtl);
  2083.         }
  2084.     }
  2085. }
  2086.  
  2087.  
  2088.  
  2089. /*****************************************************************************/
  2090.  
  2091.  
  2092.  
  2093. /* This function is used to resize a TextEdit control.  Pass it the TextEdit
  2094. ** record to resize, plus the new horizontal and vertical size.  It will
  2095. ** resize the TextEdit control, realign the text, if necessary, plus it will
  2096. ** resize and adjust any scrollbars the TextEdit control may have.  All areas
  2097. ** that need updating are cleared and invalidated. */
  2098.  
  2099. #pragma segment Controls
  2100. void    CTESize(TEHandle teHndl, short newH, short newV, Boolean newDest)
  2101. {
  2102.     WindowPtr        oldPort;
  2103.     Rect            viewRct, brdrRct, teViewRct, oldTeViewRct, rct;
  2104.     short            i, dx, dy, mode, hScroll, vScroll;
  2105.     ControlHandle    viewCtl, ctl[2];
  2106.     CTEDataHndl        teData;
  2107.     RgnHandle        rgn1, rgn2;
  2108.  
  2109.     if (!(viewCtl = CTEViewFromTE(teHndl))) return;
  2110.  
  2111.     GetPort(&oldPort);
  2112.     SetPort((*teHndl)->inPort);
  2113.  
  2114.     viewRct = (*viewCtl)->contrlRect;
  2115.     teData  = (CTEDataHndl)(*viewCtl)->contrlData;
  2116.     brdrRct = (*teData)->brdrRect;
  2117.     mode    = (*teData)->mode;
  2118.  
  2119.     dx = newH - (viewRct.right  - viewRct.left);
  2120.     dy = newV - (viewRct.bottom - viewRct.top);
  2121.     if ((!dx) && (!dy)) return;
  2122.  
  2123.     hScroll = (mode & cteHScroll) ? 1 : 0;
  2124.     vScroll = (mode & cteVScroll) ? 1 : 0;
  2125.         /* Get enough info to determine if we need to fix the scrollbar sizes. */
  2126.  
  2127.     rct = viewRct;                /* Erase all of the old list control and scrollbars. */
  2128.     if (!EmptyRect(&brdrRct))
  2129.         UnionRect(&rct, &brdrRct, &rct);
  2130.     if (vScroll)
  2131.         rct.right += 15;
  2132.     if (hScroll)
  2133.         rct.bottom += 15;
  2134.     if (mode & cteShowActive)
  2135.         InsetRect(&rct, -4, -4);
  2136.     EraseRect(&rct);
  2137.     InvalRect(&rct);
  2138.  
  2139.     for (i = 0; i < 2; ++i) {
  2140.         if (ctl[i] = CTEScrollFromView(viewCtl, i)) {
  2141.             rct = (*ctl[i])->contrlRect;
  2142.             if (i) {
  2143.                 HideControl(ctl[i]);
  2144.                 SizeControl(ctl[i], rct.right - rct.left, rct.bottom - rct.top + dy);
  2145.                 MoveControl(ctl[i], rct.left + dx, rct.top);
  2146.             }
  2147.             else {
  2148.                 HideControl(ctl[i]);
  2149.                 SizeControl(ctl[i], rct.right - rct.left + dx, rct.bottom - rct.top);
  2150.                 MoveControl(ctl[i], rct.left, rct.top + dy);
  2151.             }
  2152.         }
  2153.     }        /* Reposition the scrollbars, if we have any. */
  2154.  
  2155.     InvalRect(&viewRct);        /* Resize our view control. */
  2156.     viewRct.right  += dx;
  2157.     viewRct.bottom += dy;
  2158.     (*viewCtl)->contrlRect = viewRct;
  2159.     InvalRect(&viewRct);
  2160.  
  2161.     InvalRect(&brdrRct);        /* Resize our border rect. */
  2162.     brdrRct.right  += dx;
  2163.     brdrRct.bottom += dy;
  2164.     (*teData)->brdrRect = brdrRct;
  2165.     InvalRect(&brdrRct);
  2166.  
  2167.     teViewRct = oldTeViewRct = (*teHndl)->viewRect;        /* Resize TextEdit viewRect. */
  2168.     teViewRct.right  += dx;
  2169.     teViewRct.bottom += dy;
  2170.     (*teHndl)->viewRect = teViewRct;
  2171.  
  2172.     if (newDest) {        /* Resize TextEdit destRect, if so indicated. */
  2173.         (*teHndl)->destRect.right  += dx;
  2174.         (*teHndl)->destRect.bottom += dy;
  2175.         TECalText(teHndl);
  2176.         EraseRect(&oldTeViewRct);        /* Redraw the whole thing if destRect changes. */
  2177.         InvalRect(&oldTeViewRct);
  2178.     }
  2179.  
  2180.     rgn1 = NewRgn();
  2181.     rgn2 = NewRgn();
  2182.     RectRgn(rgn1, &oldTeViewRct);        /* Clear old areas if we are shrinking. */
  2183.     RectRgn(rgn2, &teViewRct);
  2184.     DiffRgn(rgn1, rgn2, rgn2);
  2185.     EraseRgn(rgn2);
  2186.     InvalRgn(rgn2);
  2187.     RectRgn(rgn2, &teViewRct);            /* Update new areas if we are growing. */
  2188.     DiffRgn(rgn2, rgn1, rgn2);
  2189.     InvalRgn(rgn2);
  2190.     DisposeRgn(rgn1);
  2191.     DisposeRgn(rgn2);
  2192.  
  2193.     for (i = 0; i < 2; ++i)
  2194.         if (ctl[i])
  2195.             ShowControl(ctl[i]);
  2196.                 /* Show the controls in their new position. */
  2197.  
  2198.     if (mode & (cteVScrollLessGrow - cteVScroll + cteHScrollLessGrow - cteHScroll)) {
  2199.         rct = viewRct;
  2200.         rct.top  = rct.bottom - 16;
  2201.         rct.left = rct.right - 16;
  2202.         if (mode & (cteVScrollLessGrow - cteVScroll))
  2203.             OffsetRect(&rct, 16, 0);
  2204.         if (mode & (cteHScrollLessGrow - cteHScroll))
  2205.             OffsetRect(&rct, 0, 16);
  2206.         EraseRect(&rct);
  2207.         InvalRect(&rct);
  2208.     }
  2209.  
  2210.     CTEAdjustTEBottom(teHndl);
  2211.     CTEAdjustScrollValues(teHndl);
  2212.  
  2213.     SetPort(oldPort);
  2214. }
  2215.  
  2216.  
  2217.  
  2218. /*****************************************************************************/
  2219.  
  2220.  
  2221.  
  2222. /* Swap the TextEdit text handle with the text handle passed in. */
  2223.  
  2224. #pragma segment Controls
  2225. Handle    CTESwapText(TEHandle teHndl, Handle newText, StScrpHandle styl, Boolean update)
  2226. {
  2227.     WindowPtr        oldPort;
  2228.     Handle            oldText;
  2229.     Rect            destRct, viewRct;
  2230.     TextStyle        srun;
  2231.     ScrpSTElement    s;
  2232.     ControlHandle    viewCtl;
  2233.     CTEDataHndl        teData;
  2234.     RgnHandle        oldClip, newClip;
  2235.  
  2236.     if (!teHndl) return(nil);
  2237.  
  2238.     GetPort(&oldPort);
  2239.     SetPort((*teHndl)->inPort);
  2240.  
  2241.     oldText = (*teHndl)->hText;
  2242.     HandToHand(&oldText);
  2243.  
  2244.     HLock(newText);
  2245.     TESetText(*newText, 0, teHndl);                            /* Make there be only 1 style run. */
  2246.     TESetText(*newText, GetHandleSize(newText), teHndl);    /* Put the correct text in the record. */
  2247.     DisposeHandle(newText);
  2248.  
  2249.     viewCtl = CTEViewFromTE(teHndl);
  2250.     teData  = (CTEDataHndl)(*viewCtl)->contrlData;
  2251.     if (!((*teData)->mode & cteRightJustify))
  2252.         TESetSelect(0, 0, teHndl);
  2253.  
  2254.     if (styl) {
  2255.         CTESetStylScrap(0, (*teHndl)->teLength, styl, teHndl);        /* Apply style to all text. */
  2256.         if ((*teHndl)->selStart)                                    /* Apply style to caret loc. */
  2257.             s = (*styl)->scrpStyleTab[0];
  2258.         else
  2259.             s = (*styl)->scrpStyleTab[(*styl)->scrpNStyles - 1];
  2260.         srun.tsFont  = s.scrpFont;
  2261.         srun.tsFace  = s.scrpFace;
  2262.         srun.tsSize  = s.scrpSize;
  2263.         srun.tsColor = s.scrpColor;
  2264.         TESetStyle(doAll, &srun, false, teHndl);
  2265.     }
  2266.  
  2267.     GetClip(oldClip = NewRgn());        /* Force the nullStyle to become active. */
  2268.     SetClip(newClip = NewRgn());        /* This also resizes the caret to reflect nullStyle size. */
  2269.     TEKey(' ', teHndl);
  2270.     TEKey(8, teHndl);
  2271.     SetClip(oldClip);
  2272.     DisposeRgn(oldClip);
  2273.     DisposeRgn(newClip);
  2274.  
  2275.     TECalText(teHndl);
  2276.  
  2277.     if (update) {
  2278.         destRct = (*teHndl)->destRect;
  2279.         viewRct = (*teHndl)->viewRect;
  2280.  
  2281.         OffsetRect(&destRct,
  2282.             viewRct.left - destRct.left,
  2283.             viewRct.top  - destRct.top);
  2284.  
  2285.         (*teHndl)->destRect = destRct;
  2286.         (*teHndl)->selStart = (*teHndl)->selEnd = 0;
  2287.  
  2288.         CTEUpdate(teHndl, CTEViewFromTE(teHndl), false);
  2289.     }
  2290.  
  2291.     CTEAdjustTEBottom(teHndl);        /* Fix these things up, whether showing or not. */
  2292.     CTEAdjustScrollValues(teHndl);
  2293.  
  2294.     SetPort(oldPort);
  2295.     return(oldText);
  2296. }
  2297.  
  2298.  
  2299.  
  2300. /*****************************************************************************/
  2301.  
  2302.  
  2303.  
  2304. /* Return information for the currently active TextEdit control.  The currently
  2305. ** active TextEdit control is stored in gActiveTEHndl, and can be accessed
  2306. ** directly.  If gActiveTEHndl is nil, then there is no currently active one.
  2307. ** The information that we return is the viewRect and window of the active
  2308. ** TextEdit control.  This is information that could be gotten directly, but
  2309. ** this call makes it a little more convenient. */
  2310.  
  2311. #pragma segment Controls
  2312. WindowPtr    CTETargetInfo(TEHandle *teHndl, Rect *teView)
  2313. {
  2314.     if (teView)
  2315.         SetRect(teView, 0, 0, 0, 0);
  2316.  
  2317.     if (teHndl)
  2318.         *teHndl = gActiveTEHndl;
  2319.  
  2320.     if (!gActiveTEHndl) return(nil);
  2321.  
  2322.     if (teView)
  2323.         *teView = (*gActiveTEHndl)->viewRect;
  2324.  
  2325.     return((*gActiveTEHndl)->inPort);
  2326. }
  2327.  
  2328.  
  2329.  
  2330. /*****************************************************************************/
  2331.  
  2332.  
  2333.  
  2334. /* Perform an undo function for the TextEdit control. */
  2335.  
  2336. #pragma segment Controls
  2337. void    CTEUndo(void)
  2338. {
  2339.     TEHandle        teHndl;
  2340.     ControlHandle    viewCtl;
  2341.     CTEDataHndl        teData;
  2342.     Handle            hText, uText;
  2343.     StScrpHandle    hStyl, uStyl;
  2344.     short            oldStart, oldEnd;
  2345.  
  2346.     if (!(teHndl = gActiveTEHndl)) return;
  2347.  
  2348.     if (viewCtl = CTEViewFromTE(teHndl)) {
  2349.         oldStart = (*teHndl)->selStart;
  2350.         oldEnd   = (*teHndl)->selEnd;
  2351.  
  2352.         (*teHndl)->selStart = (*teHndl)->selEnd = 0;
  2353.  
  2354.         teData = (CTEDataHndl)(*viewCtl)->contrlData;
  2355.         hText  = (*teHndl)->hText;
  2356.         uText  = (*teData)->undoText;
  2357.         if (!uText) return;        /* There is no undo yet.  Getting called in this state is
  2358.                                 ** actually an error, but we check, just in case. */
  2359.  
  2360.         hStyl = CTEGetFullStylScrap(teHndl);
  2361.         uStyl = (*teData)->undoStyl;
  2362.  
  2363.         (*teData)->undoText = CTESwapText(teHndl, (*teData)->undoText, uStyl, false);
  2364.         DisposeHandle((Handle)uStyl);
  2365.         (*teData)->undoStyl = hStyl;
  2366.  
  2367.         CTEUpdate(teHndl, viewCtl, false);
  2368.         TESetSelect((*teData)->undoSelStart, (*teData)->undoSelEnd, teHndl);
  2369.         CTEAdjustTEBottom(teHndl);
  2370.         CTEAdjustScrollValues(teHndl);
  2371.  
  2372.         (*teData)->newUndo      = true;
  2373.         (*teData)->undoSelStart = oldStart;
  2374.         (*teData)->undoSelEnd   = oldEnd;
  2375.     }
  2376. }
  2377.  
  2378.  
  2379.  
  2380. /*****************************************************************************/
  2381.  
  2382.  
  2383.  
  2384. /* Draw the TextEdit control and frame. */
  2385.  
  2386. #pragma segment Controls
  2387. void    CTEUpdate(TEHandle teHndl, ControlHandle ctl, Boolean justShowActive)
  2388. {
  2389.     WindowPtr    oldPort, tePort;
  2390.     Rect        viewRct, brdrRct;
  2391.     CTEDataHndl    teData;
  2392.     short        mode;
  2393.  
  2394.     if (teHndl) {
  2395.         if ((*ctl)->contrlVis) {
  2396.  
  2397.             GetPort(&oldPort);
  2398.             SetPort(tePort = (*teHndl)->inPort);
  2399.  
  2400.             teData  = (CTEDataHndl)(*ctl)->contrlData;
  2401.             mode    = (*teData)->mode;
  2402.             viewRct = (*teHndl)->viewRect;
  2403.             brdrRct = (*teData)->brdrRect;
  2404.             if (!(mode & cteNoBorder))
  2405.                 if (!EmptyRect(&brdrRct))
  2406.                     FrameRect(&brdrRct);
  2407.  
  2408.             if (!justShowActive) {
  2409.                 EraseRect(&viewRct);
  2410.                 TEUpdate(&viewRct, teHndl);
  2411.             }
  2412.     
  2413.             if (mode & cteShowActive) {
  2414.                 if (!EmptyRect(&brdrRct)) {
  2415.                     InsetRect(&brdrRct, -3, -3);
  2416.                     if (mode & cteVScroll)
  2417.                         brdrRct.right  += 15;
  2418.                     if (mode & cteHScroll)
  2419.                         brdrRct.bottom += 15;
  2420.                     if ((!((WindowPeek)tePort)->hilited) || (teHndl != gActiveTEHndl))
  2421.                         PenPat((ConstPatternParam)&qd.white);
  2422.                     PenSize(2, 2);
  2423.                     FrameRect(&brdrRct);
  2424.                     PenNormal();
  2425.                 }
  2426.             }
  2427.  
  2428.             SetPort(oldPort);
  2429.         }
  2430.     }
  2431. }
  2432.  
  2433.  
  2434.  
  2435. /*****************************************************************************/
  2436.  
  2437.  
  2438.  
  2439. /* Return the control handle for the view control that owns the TextEdit
  2440. ** record.  Use this to find the view to do customizations such as changing
  2441. ** the update procedure for this TextEdit control. */
  2442.  
  2443. #pragma segment Controls
  2444. ControlHandle    CTEViewFromTE(TEHandle teHndl)
  2445. {
  2446.     WindowPtr        window;
  2447.     ControlHandle    viewCtl;
  2448.     TEHandle        te;
  2449.  
  2450.     if (!teHndl) return(nil);
  2451.  
  2452.     window = (WindowPtr)(*teHndl)->inPort;
  2453.  
  2454.     for (viewCtl = nil;;) {
  2455.  
  2456.         viewCtl = CTENext(window, &te, viewCtl, 1, false);
  2457.         if ((!viewCtl) || (te == teHndl)) return(viewCtl);
  2458.     }
  2459. }
  2460.  
  2461.  
  2462.  
  2463. static ControlHandle    dummyCTEViewFromTE(TEHandle teHndl)
  2464. {
  2465. #pragma unused (teHndl)
  2466.     return(nil);
  2467. }
  2468.  
  2469.  
  2470.  
  2471. /*****************************************************************************/
  2472.  
  2473.  
  2474.  
  2475. /* Call this when a window with TextEdit controls is being activated.  This
  2476. ** will make the TextEdit control that was last active in this window the
  2477. ** active TextEdit control again.  If it can't find one that was the last
  2478. ** active control, it makes the first one it finds the active control. */
  2479.  
  2480. #pragma segment Controls
  2481. TEHandle    CTEWindActivate(WindowPtr window, Boolean displayIt)
  2482. {
  2483.     short            hilite, scrollNum;
  2484.     ControlHandle    viewCtl, scrollCtl;
  2485.     TEHandle        te;
  2486.     CTEDataHndl        teData;
  2487.  
  2488.     if (!window) return(nil);
  2489.  
  2490.     hilite = 255;
  2491.     if (((WindowPeek)window)->hilited)
  2492.         hilite = 0;
  2493.  
  2494.     for (viewCtl = nil; viewCtl = CTENext(window, &te, viewCtl, 1, true);) {
  2495.  
  2496.         teData = (CTEDataHndl)(*viewCtl)->contrlData;
  2497.         if (!((*teData)->mode & cteActive)) continue;
  2498.  
  2499.         if (displayIt) {
  2500.  
  2501.             if (!hilite)
  2502.                 CTEActivate(true, te);
  2503.             else
  2504.                 CTEActivate(false, te);
  2505.  
  2506.             for (scrollNum = 0; scrollNum < 2; ++scrollNum) {
  2507.                 scrollCtl = CTEScrollFromTE(te, scrollNum);
  2508.                 if (scrollCtl)
  2509.                     HiliteControl(scrollCtl, hilite);
  2510.             }
  2511.         }
  2512.  
  2513.         if (hilite)
  2514.             te = nil;
  2515.  
  2516.         return(te);
  2517.     }
  2518.  
  2519.     return(nil);
  2520. }
  2521.  
  2522.  
  2523.  
  2524. static TEHandle    dummyCTEWindActivate(WindowPtr window, Boolean displayIt)
  2525. {
  2526. #pragma unused (window, displayIt)
  2527.  
  2528.     return(nil);
  2529. }
  2530.  
  2531.  
  2532.  
  2533. /*****************************************************************************/
  2534.  
  2535.  
  2536.  
  2537. /* This function is called after an edit to make sure that there is no extra
  2538. ** white space at the bottom of the viewRect.  If there are blank lines at
  2539. ** the bottom of the viewRect, and there is text scrolled off the top of the
  2540. ** viewRect, then the TextEdit control is scrolled to fill this space, or as
  2541. ** much of it as possible. */
  2542.  
  2543. #pragma segment Controls
  2544. void    CTEAdjustTEBottom(TEHandle teHndl)
  2545. {
  2546.     Rect    destRct, viewRct;
  2547.     short    botDiff, topDiff;
  2548.  
  2549.     destRct = (*teHndl)->destRect;
  2550.     viewRct = (*teHndl)->viewRect;
  2551.     destRct.bottom = destRct.top + CTEDocHeight(teHndl);
  2552.  
  2553.     botDiff = viewRct.bottom - destRct.bottom;
  2554.  
  2555.     if (botDiff > 0) {
  2556.         topDiff = viewRct.top - destRct.top;
  2557.         if (botDiff > topDiff)
  2558.             botDiff = topDiff;
  2559.         if (botDiff)
  2560.             TEScroll(0, botDiff, teHndl);
  2561.     }
  2562. }
  2563.  
  2564.  
  2565.  
  2566. /*****************************************************************************/
  2567.  
  2568.  
  2569.  
  2570. /* Bring the scrollbar values up to date with the current document position
  2571. ** and length. */
  2572.  
  2573. #pragma segment Controls
  2574. void    CTEAdjustScrollValues(TEHandle teHndl)
  2575. {
  2576.     short            scrollNum;
  2577.     ControlHandle    scrollCtl;
  2578.  
  2579.     for (scrollNum = 0; scrollNum < 2; ++scrollNum) {
  2580.         scrollCtl = CTEScrollFromTE(teHndl, scrollNum);
  2581.         if (scrollCtl)
  2582.             AdjustOneScrollValue(teHndl, scrollCtl, scrollNum);
  2583.     }
  2584. }
  2585.  
  2586.  
  2587.  
  2588. /*****************************************************************************/
  2589.  
  2590.  
  2591.  
  2592. #pragma segment Controls
  2593. StScrpHandle    CTEGetFullStylScrap(TEHandle teHndl)
  2594. {
  2595.     short            selStart, selEnd;
  2596.     StScrpHandle    styl;
  2597.  
  2598.     selStart = (*teHndl)->selStart;
  2599.     selEnd   = (*teHndl)->selEnd;
  2600.  
  2601.     (*teHndl)->selStart = 0;
  2602.     (*teHndl)->selEnd   = (*teHndl)->teLength;
  2603.  
  2604.     styl = GetStylScrap(teHndl);
  2605.  
  2606.     (*teHndl)->selStart = selStart;
  2607.     (*teHndl)->selEnd   = selEnd;
  2608.  
  2609.     return(styl);
  2610. }
  2611.  
  2612.  
  2613.  
  2614. /*****************************************************************************/
  2615.  
  2616.  
  2617.  
  2618. #pragma segment Controls
  2619. void    CTESetStylScrap(short begRng, short endRng, StScrpHandle styles, TEHandle teHndl)
  2620. {
  2621.     short            n, i, b, e, selStart, selEnd;
  2622.     ScrpSTElement    styl, s;
  2623.     TextStyle        srun;
  2624.     TEStyleHandle    shndl;
  2625.  
  2626.     if (!styles) return;
  2627.  
  2628.     shndl = GetStylHandle(teHndl);
  2629.  
  2630.     selStart = (*teHndl)->selStart;
  2631.     selEnd   = (*teHndl)->selEnd;
  2632.  
  2633.     n = (*styles)->scrpNStyles;
  2634.  
  2635.     for (i = 0; i < n;) {
  2636.         styl = (*styles)->scrpStyleTab[i++];
  2637.         b  = styl.scrpStartChar;
  2638.         if (i == n)
  2639.             e = endRng;
  2640.         else {
  2641.             s = (*styles)->scrpStyleTab[i];
  2642.             e = s.scrpStartChar;
  2643.         }
  2644.         if (b >= endRng) break;            /* We're past the range for style application. */
  2645.         if (e <= begRng) continue;        /* We're not to the range for style application. */
  2646.  
  2647.         if (b < begRng) b = begRng;        /* Clip to range for style application. */
  2648.         if (e > endRng) e = endRng;
  2649.  
  2650.         if (b < e) {
  2651.             srun.tsFont  = styl.scrpFont;
  2652.             srun.tsFace  = styl.scrpFace;
  2653.             srun.tsSize  = styl.scrpSize;
  2654.             srun.tsColor = styl.scrpColor;
  2655.             (*teHndl)->selStart = b;
  2656.             (*teHndl)->selEnd   = e;
  2657.             TESetStyle(doAll, &srun, false, teHndl);
  2658.             TECalText(teHndl);
  2659.         }
  2660.     }
  2661.  
  2662.     (*teHndl)->selStart = selStart;
  2663.     (*teHndl)->selEnd   = selEnd;
  2664. }
  2665.  
  2666.  
  2667.  
  2668. /*****************************************************************************/
  2669.  
  2670.  
  2671.  
  2672. #pragma segment Controls
  2673. short    CTEGetLineNum(TEHandle te, short offset)
  2674. {
  2675.     short    i;
  2676.  
  2677.     for (i = 0; i < (*te)->nLines; ++i)
  2678.         if ((*te)->lineStarts[i] > offset)
  2679.             break;
  2680.  
  2681.     return(i);
  2682. }
  2683.  
  2684.  
  2685.  
  2686. /*****************************************************************************/
  2687.  
  2688.  
  2689.  
  2690. #pragma segment Controls
  2691. short    CTEGetLineHeight(TEHandle te, short lineNum, short *ascent)
  2692. {
  2693.     TEStyleHandle    tes;
  2694.     LHHandle        lhh;
  2695.  
  2696.     tes = *(TEStyleHandle *)&((*te)->txFont);
  2697.     lhh = (*tes)->lhTab;
  2698.  
  2699.     if (ascent) *ascent = (*lhh)[lineNum - 1].lhAscent;
  2700.     return((*lhh)[--lineNum].lhHeight);
  2701. }
  2702.  
  2703.  
  2704.  
  2705. /*****************************************************************************/
  2706.  
  2707.  
  2708.  
  2709. #pragma segment Controls
  2710. void    CTEGetPStr(ControlHandle ctl, StringPtr pstr)
  2711. {
  2712.     TEHandle        te;
  2713.     unsigned short    len;
  2714.  
  2715.     if (!ctl) return;
  2716.     te = (TEHandle)GetCRefCon(ctl);
  2717.     if (!te) return;
  2718.  
  2719.     len = (*te)->teLength;
  2720.     if (len > 255) len = 255;
  2721.  
  2722.     BlockMove(*(*te)->hText, pstr + 1, *pstr = len);
  2723. }
  2724.  
  2725.  
  2726.  
  2727. /*****************************************************************************/
  2728.  
  2729.  
  2730.  
  2731. #pragma segment Controls
  2732. void    CTESetPStr(ControlHandle ctl, StringPtr pstr)
  2733. {
  2734.     TEHandle    te;
  2735.     Handle        h;
  2736.  
  2737.     if (!ctl) return;
  2738.     te = (TEHandle)GetCRefCon(ctl);
  2739.     if (!te) return;
  2740.  
  2741.     if (h = NewHandle(pstr[0])) {
  2742.         BlockMove(pstr + 1, *h, pstr[0]);
  2743.         DisposeHandle(CTESwapText(te, h, nil, true));
  2744.     }
  2745. }
  2746.  
  2747.  
  2748.  
  2749. /*****************************************************************************/
  2750. /*****************************************************************************/
  2751.  
  2752.  
  2753.  
  2754. #pragma segment Controls
  2755. static pascal void    VActionProc(ControlHandle scrollCtl, short part)
  2756. {
  2757.     short        delta, value, teOffset;
  2758.     short        oldValue, max;
  2759.     TEHandle    te;
  2760.     
  2761.     if (part) {                        /* If it was actually in the control. */
  2762.  
  2763.         te = gActiveTEHndl;
  2764.         switch (part) {
  2765.             case inUpButton:
  2766.             case inDownButton:        /* One line. */
  2767.                 delta = 16;
  2768.                 break;
  2769.             case inPageUp:            /* One page. */
  2770.             case inPageDown:
  2771.                 delta = (*te)->viewRect.bottom - (*te)->viewRect.top;
  2772.                 if (delta > 16)
  2773.                     delta -= 16;
  2774.                 break;
  2775.         }
  2776.         if ( (part == inUpButton) || (part == inPageUp) )
  2777.             delta = -delta;        /* Reverse direction for an upper. */
  2778.  
  2779.         value = (oldValue = GetCtlValue(scrollCtl)) + delta;
  2780.         if (value < 0)
  2781.             value = 0;
  2782.         if (value > (max = GetCtlMax(scrollCtl)))
  2783.             value = max;
  2784.  
  2785.         if (value != oldValue) {
  2786.             SetCtlValue(scrollCtl, value);
  2787.             teOffset = (*te)->viewRect.top - (*te)->destRect.top;
  2788.             if (value -= teOffset)
  2789.                 TEScroll(0, -value, te);
  2790.         }
  2791.     }
  2792. }
  2793.  
  2794.  
  2795.  
  2796. /*****************************************************************************/
  2797.  
  2798.  
  2799.  
  2800. #pragma segment Controls
  2801. static pascal void    HActionProc(ControlHandle scrollCtl, short part)
  2802. {
  2803.     short        delta, value, teOffset;
  2804.     short        oldValue, max;
  2805.     TEHandle    te;
  2806.     
  2807.     if (part) {                        /* If it was actually in the control. */
  2808.  
  2809.         te = gActiveTEHndl;
  2810.         switch (part) {
  2811.             case inUpButton:
  2812.             case inDownButton:        /* One line. */
  2813.                 delta = 16;
  2814.                 break;
  2815.             case inPageUp:            /* One page. */
  2816.             case inPageDown:
  2817.                 delta = (*te)->viewRect.right - (*te)->viewRect.left;
  2818.                 if (delta > 16)
  2819.                     delta -= 16;
  2820.                 break;
  2821.         }
  2822.         if ( (part == inUpButton) || (part == inPageUp) )
  2823.             delta = -delta;        /* Reverse direction for an upper. */
  2824.  
  2825.         value = (oldValue = GetCtlValue(scrollCtl)) + delta;
  2826.         if (value < 0)
  2827.             value = 0;
  2828.         if (value > (max = GetCtlMax(scrollCtl)))
  2829.             value = max;
  2830.  
  2831.         if (value != oldValue) {
  2832.             SetCtlValue(scrollCtl, value);
  2833.             teOffset = (*te)->viewRect.left - (*te)->destRect.left;
  2834.             if (value -= teOffset)
  2835.                 TEScroll(-value, 0, te);
  2836.         }
  2837.     }
  2838. }
  2839.  
  2840.  
  2841.  
  2842. /*****************************************************************************/
  2843.  
  2844.  
  2845.  
  2846. /* Bring one scrollbar value up to date with the current document position
  2847. ** and length. */
  2848.  
  2849. #pragma segment Controls
  2850. static void    AdjustOneScrollValue(TEHandle teHndl, ControlHandle ctl, Boolean vert)
  2851. {
  2852.     Boolean    front;
  2853.     short    textPix, viewPix;
  2854.     short    max, oldMax, value, oldValue;
  2855.  
  2856.     front = ((WindowPeek)(*ctl)->contrlOwner)->hilited;
  2857.  
  2858.     oldValue = GetCtlValue(ctl);
  2859.     oldMax   = GetCtlMax(ctl);
  2860.  
  2861.     if (vert) {
  2862.         textPix = CTEDocHeight(teHndl);
  2863.         viewPix = (*teHndl)->viewRect.bottom - (*teHndl)->viewRect.top;
  2864.     }
  2865.     else {
  2866.         textPix = (*teHndl)->destRect.right - (*teHndl)->destRect.left;
  2867.         viewPix = (*teHndl)->viewRect.right - (*teHndl)->viewRect.left;
  2868.     }
  2869.     max = textPix - viewPix;
  2870.  
  2871.     if (max < 0)
  2872.         max = 0;
  2873.     if (max != oldMax) {
  2874.         if (front)
  2875.             SetCtlMax(ctl, max);
  2876.         else
  2877.             (*ctl)->contrlMax = max;
  2878.     }
  2879.  
  2880.     if (vert)
  2881.         value = (*teHndl)->viewRect.top  - (*teHndl)->destRect.top;
  2882.     else
  2883.         value = (*teHndl)->viewRect.left - (*teHndl)->destRect.left;
  2884.  
  2885.     if (value < 0)
  2886.         value = 0;
  2887.     if (value > max)
  2888.         value = max;
  2889.     if (value != oldValue) {
  2890.         if (front)
  2891.             SetCtlValue(ctl, value);
  2892.         else
  2893.             (*ctl)->contrlValue = value;
  2894.     }
  2895. }
  2896.  
  2897.  
  2898.  
  2899. /*****************************************************************************/
  2900.  
  2901.  
  2902.  
  2903. #pragma segment Controls
  2904. Boolean    TSMTEAvailable(void)
  2905. {
  2906.     long    response;
  2907.     
  2908.     if (Gestalt(kTSMTESignature, &response)) return(false);
  2909.  
  2910.     return((response & (1 << gestaltTSMTE)) != 0);
  2911. }
  2912.  
  2913.  
  2914.  
  2915. /*****************************************************************************/
  2916.  
  2917.  
  2918.  
  2919. #pragma segment Controls
  2920. static pascal void        TSMTEUpdateProc(TEHandle te, long fixLen, long inputAreaStart,
  2921.                                         long inputAreaEnd, long pinStart, long pinEnd, long refCon)
  2922. {
  2923. #pragma unused (fixLen, inputAreaStart, inputAreaEnd, pinStart, pinEnd, refCon)
  2924.  
  2925.     CTEAdjustTEBottom(te);
  2926.     CTEAdjustScrollValues(te);
  2927. }
  2928.  
  2929.  
  2930.  
  2931.